How many bits are use in bit manipulation in python?

Viewed 187

The int data type in Java has 32 bits, hence when we do bit manipulation in Java it will happen in 32 bits. But there is no limit in Python for integer data type, so for how many bits does bit manipulation take place in python?

For example 2 & 3 in java will take place in 32 bits that is

0000 0000 0000 0000 0000 0000 0000 0010 & 0000 0000 0000 0000 0000 0000 0000 0011

But the same will happen for how many bits in python

i.e.. will it be 10 & 11

or 0010 & 0011

or what

3 Answers

Just like with other integer operations, bit manipulation has unlimited precision.

>>> (0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef ^ 
...  0x64004ee264004ee264004ee264004ee264004ee264004ee264004ee264004ee2 == 
...  0xbaadf00dbaadf00dbaadf00dbaadf00dbaadf00dbaadf00dbaadf00dbaadf00d)
True

If you're trying to match the output of routines in other languages you can clamp results to a certain number of bits by masking with an appropriate bitmask. result & 0xff for 1 byte, result & 0xffff for 2 bytes, result & 0xffffffff for 4 bytes, etc.

...will it be 10 & 11 or 0010 & 0011?

There is no difference. 0b10 == 0b0010 and 0b11 == 0b0011. They are the exact same numbers; zero padding on the left doesn't change anything. You can think of these numbers as having an infinite number of zeros to the left.

I'm not being deliberately obtuse, I promise you. You're asking, "Is it A or B?" and the answer is, "Both. A and B are the same thing."

In Java or C++ or C# or any other language with fixed-width integer types, the fact that they perform 16-bit or 32-bit or 64-bit bit manipulation has observable consequences on the behavior of your code. Output results get truncated to the bit width of the input types. You always have to think about overflow and how numbers might wrap around. Adding two numbers might result in a smaller number. The fixed width bit manipulation has a visible effect that you must think about.

Python uses unlimited precision integers, so there's no such thing. Yes, if you peek under the hood at how the unlimited precision integer library works, it will used fixed size integers somewhere in there. It will ultimately execute assembly instructions that do 32-bit ANDs or 64-bit XORs or whatever the case may be. But all of that is internal to Python. It's not visible externally to us, the users. The externally visible behavior is that integers appear to have unlimited precision. We can think of every number as conceptually having an infinite number of 0 bits to the left (or an infinite number of 1 bits, if the number is negative).

Well, this is a really interesting question.
I'm not sure about the answer, however, I'm thinking that it might depend on the larger number's bits.
So for example :

a = 2
b = 3
print(bin(a))
print(bin(b))
print(a&b)
b=11
print(bin(a))
print(bin(b))
print(a&b)

Then the output is :

0b10
0b11
2
0b10
0b1011
2

The case where b is 3, it will take 2 bits.

2 = 10
3 = 11

In the case where b is 11, it will take 4 bits.

2 = 0010
11 = 1011

So this is what I've observed, please tell me if I'm dead wrong or not.

Please correct me if I'm wrong

Prior to Python 3.1, there was no easy way to determine how Python represented a specific integer internally, i.e. how many bits were used. Python 3.1 adds a bit_length() method to the int type that does exactly that.

resource https://wiki.python.org/moin/BitManipulation

x=7
y=30
print( (x|y).bit_length() )

Output : 5

Related