Aug-07-2018, 10:05 AM
(This post was last modified: Aug-07-2018, 10:05 AM by Gribouillis.)
In python, integers conceptually have a semi-infinite number of bits, so for example if
For example the sequence of bits of -23762 is the two's complement of the bits of 23761. You can confirm this by using a 32 bits mask for example
>>> bin(37) '0b100101'the integer 37 is represented by the infinite sequence
...000000100101
. It means that the two's complement ~37
is a sequence starting with an infinite number of 1's, but of course it is not stored this way. Instead, it is stored as the negative number -38
. For every integer, one hasassert ~x == -(x+1)So you don't see the infinite sequence, but with regards to bitwise operations, you need to consider negative integers as a sequence of bits starting with an infinite number of 1s.
For example the sequence of bits of -23762 is the two's complement of the bits of 23761. You can confirm this by using a 32 bits mask for example
>>> mask = (1 << 32) - 1 >>> bin(mask) '0b11111111111111111111111111111111' >>> bin(23761) '0b101110011010001' >>> bin(-23762 & mask) '0b11111111111111111010001100101110'