mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
fix comptime bitwise operations with negative values
closes #1387 closes #1529
This commit is contained in:
parent
422269ea6e
commit
32c91ad892
@ -50,7 +50,7 @@ size_t bigint_bits_needed(const BigInt *op) {
|
||||
size_t full_bits = op->digit_count * 64;
|
||||
size_t leading_zero_count = bigint_clz(op, full_bits);
|
||||
size_t bits_needed = full_bits - leading_zero_count;
|
||||
return bits_needed + op->is_negative;
|
||||
return bits_needed;
|
||||
}
|
||||
|
||||
static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count) {
|
||||
|
||||
@ -737,3 +737,16 @@ test "slice bounds in comptime concatenation" {
|
||||
assert(str2.len == 1);
|
||||
assert(std.mem.eql(u8, str2, "1"));
|
||||
}
|
||||
|
||||
test "comptime bitwise operators" {
|
||||
comptime {
|
||||
assert(3 & 1 == 1);
|
||||
assert(3 & -1 == 3);
|
||||
assert(-3 & -1 == -3);
|
||||
assert(3 | -1 == -1);
|
||||
assert(-3 | -1 == -1);
|
||||
assert(3 ^ -1 == -4);
|
||||
assert(~i8(-1) == 0);
|
||||
assert(~i128(-1) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user