1

Since python has no limit on the number of bits for storing an integer, how exactly does it perform bitwise operations on integers?

How many bits does it really store an integer in?

RealPython claims that there is no signed bit in python.

Then why is it that when the following code is executed, the sign is retained and the answer is -1?

print(-2 >> 1)
1
  • 2
    As stated in the doc, "The result of bitwise operations is calculated as though carried out in two’s complement with an infinite number of sign bits." For your example, when shifting right, the new bits you introduce from the left just have to be the same as the most significant bit before the shift. Commented Feb 13, 2024 at 7:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.