fix comptime bitwise operations with negative values

closes #1387
closes #1529
This commit is contained in:
Andrew Kelley 2018-09-24 14:37:55 -04:00
parent 422269ea6e
commit 32c91ad892
No known key found for this signature in database
GPG Key ID: 4E7CD66038A4D47C
2 changed files with 14 additions and 1 deletions

View File

@ -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) {

View File

@ -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);
}
}