Skip to content

allow binary not, binary and, binary or, binary xor, and boolean not operators on vectors of bool #24093

Closed
@andrewrk

Description

@andrewrk
const std = @import("std");
const expect = std.testing.expect;

test "binary not operator on vector of bool" {
    const v1: @Vector(2, bool) = .{ true, false };
    const v2 = ~v1;
    try expect(!v2[0]);
    try expect(v2[1]);
}

test "boolean not operator on vector of bool" {
    const v1: @Vector(2, bool) = .{ true, false };
    const v2 = !v1;
    try expect(!v2[0]);
    try expect(v2[1]);
}

test "binary and operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 & v2;
    try expect(!v3[0]);
    try expect(!v3[1]);
    try expect(v3[2]);
    try expect(!v3[3]);
}

test "binary or operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 & v2;
    try expect(v3[0]);
    try expect(!v3[1]);
    try expect(v3[2]);
    try expect(v3[3]);
}

test "binary xor operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 ^ v2;
    try expect(v3[0]);
    try expect(!v3[1]);
    try expect(!v3[2]);
    try expect(v3[3]);
}

test "boolean neq operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 != v2;
    try expect(v3[0]);
    try expect(!v3[1]);
    try expect(!v3[2]);
    try expect(v3[3]);
}

Given that:

  • ! and ~ will be the same for bools.
  • != and ^ will be the same for bools.

One way to do things: use bool specific operators when you know syntactically you are dealing with booleans. Use the bitwise alternative for generic code.

Related:

As additional criteria to close this issue please adjust the langref so that it is unambiguous that and and or cannot be used with vectors of bool, for instance by adjusting this text:

Vectors support the same builtin operators as their underlying base types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions