Ask Your Question

Revision history [back]

The bitwise not in python is supposed to be ~ (see https://wiki.python.org/moin/BitwiseOperators about how negative numbers are encoded), but Sage's preparser first transforms 0b1000 as an element of ZZ for which the action of ~ is the multiplicative inverse:

sage: ~0b1000
1/8

This is because, in Sage:

sage: 0b1000.parent()
Integer Ring
sage: preparse("0b1000")
"Integer('1000', 2)"

You can get the python behaviour you can either turn the sage preparser off:

sage: preparser(False)
sage: ~0b1000
-9

or explicitely use python int:

sage: ~int(0b1000)
-9

Another workaround is to notice that not a is the same as a xor 1.

The bitwise not not in python Python is supposed to be ~ (see https://wiki.python.org/moin/BitwiseOperators about how negative numbers are encoded), but Sage's preparser first transforms 0b1000 as an element of ZZ for which the action of ~ is the multiplicative inverse:

sage: ~0b1000
1/8

This is because, in Sage:

sage: 0b1000.parent()
Integer Ring
sage: preparse("0b1000")
"Integer('1000', 2)"

You can To get the python behaviour behaviour, you can either turn the sage preparser off:

sage: preparser(False)
sage: ~0b1000
-9

or explicitely use python int:

sage: ~int(0b1000)
-9

Another workaround is to notice that not a is the same as a xor 1.