From ba776a3c9fbaa8f64f1567112c71c93416776260 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 15 Jun 2018 23:01:12 +0100 Subject: [PATCH] Fix bitwise not for signed integers --- src/check_expr.cpp | 2 +- src/exact_value.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 1529010ca..6d00087e2 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1465,7 +1465,7 @@ void check_unary_expr(CheckerContext *c, Operand *o, Token op, AstNode *node) { return; } - o->value = exact_unary_operator_value(op.kind, o->value, precision); + o->value = exact_unary_operator_value(op.kind, o->value, precision, is_type_unsigned(type)); if (is_type_typed(type)) { if (node != nullptr) { diff --git a/src/exact_value.cpp b/src/exact_value.cpp index 1fe0206d8..1760d05c2 100644 --- a/src/exact_value.cpp +++ b/src/exact_value.cpp @@ -353,7 +353,7 @@ ExactValue exact_value_make_imag(ExactValue v) { } -ExactValue exact_unary_operator_value(TokenKind op, ExactValue v, i32 precision) { +ExactValue exact_unary_operator_value(TokenKind op, ExactValue v, i32 precision, bool is_unsigned) { switch (op) { case Token_Add: { switch (v.kind) { @@ -404,7 +404,9 @@ ExactValue exact_unary_operator_value(TokenKind op, ExactValue v, i32 precision) // NOTE(bill): unsigned integers will be negative and will need to be // limited to the types precision // IMPORTANT NOTE(bill): Max precision is 64 bits as that's how integers are stored - i = i & unsigned_integer_maxs[precision/8]; + if (is_unsigned) { + i = i & unsigned_integer_maxs[precision/8]; + } // if (0 < precision && precision < 64) { // i = i & ~(-1ll << precision); // }