authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-24 14:37:55-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-24 14:38:51-04:00
log32c91ad892f5dc0138828aea19b3c0e2b062d168
tree03d8101eff6e5340ccce1b3f39179465dcc2d240
parent422269ea6e49409464fd1fd00212fc7f7681a5e0
signaturelock-open Commit is signed but in an unrecognized format.

fix comptime bitwise operations with negative values

closes #1387 closes #1529

2 files changed, 14 insertions(+), 1 deletions(-)

src/bigint.cpp+1-1
......@@ -50,7 +50,7 @@ size_t bigint_bits_needed(const BigInt *op) {
5050 size_t full_bits = op->digit_count * 64;
5151 size_t leading_zero_count = bigint_clz(op, full_bits);
5252 size_t bits_needed = full_bits - leading_zero_count;
53 return bits_needed + op->is_negative;
53 return bits_needed;
5454}
5555
5656static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count) {
test/cases/eval.zig+13
......@@ -737,3 +737,16 @@ test "slice bounds in comptime concatenation" {
737737 assert(str2.len == 1);
738738 assert(std.mem.eql(u8, str2, "1"));
739739}
740
741test "comptime bitwise operators" {
742 comptime {
743 assert(3 & 1 == 1);
744 assert(3 & -1 == 3);
745 assert(-3 & -1 == -3);
746 assert(3 | -1 == -1);
747 assert(-3 | -1 == -1);
748 assert(3 ^ -1 == -4);
749 assert(~i8(-1) == 0);
750 assert(~i128(-1) == 0);
751 }
752}