Commit 9b5b8c92 authored by Josh Tobin's avatar Josh Tobin Committed by Stefan Behnel

Fix bitwise "&" operator with negative operand (GH-3312)

parent 90ca446a
...@@ -923,7 +923,11 @@ static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, CYTHON_UNUSED ...@@ -923,7 +923,11 @@ static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, CYTHON_UNUSED
{{if c_op == '&'}} {{if c_op == '&'}}
// special case for &-ing arbitrarily large numbers with known single digit operands // special case for &-ing arbitrarily large numbers with known single digit operands
if ((intval & PyLong_MASK) == intval) { if ((intval & PyLong_MASK) == intval) {
return PyLong_FromLong(likely(size) ? digits[0] & intval : 0); long result = 0;
if(likely(size)) {
result = intval & (likely(size>0) ? digits[0] : (PyLong_MASK - digits[0] + 1));
}
return PyLong_FromLong(result);
} }
{{endif}} {{endif}}
// special cases for 0: + - * % / // | ^ & >> << // special cases for 0: + - * % / // | ^ & >> <<
......
...@@ -79,6 +79,8 @@ def and_int(obj2): ...@@ -79,6 +79,8 @@ def and_int(obj2):
0 0
>>> and_int(18) >>> and_int(18)
16 16
>>> and_int(-1)
16
""" """
obj1 = obj2 & 0x10 obj1 = obj2 & 0x10
return obj1 return obj1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment