Closed
Description
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.