Python Binary Integer: Bits and Bitwise Operations
Learn how Python represents integers in binary, how bitwise operators behave, and how to use them for practical bit manipulation.

When you work with a Python binary integer, you are using Python's arbitrary-precision int type, but its bitwise behavior is easiest to understand as an infinite two's complement representation. Unlike fixed-width integers in languages like C or Java, Python integers can grow beyond 64 bits; negative numbers behave as though they have infinitely many leading 1 bits. Understanding this representation is essential for writing correct bit manipulation code, especially when dealing with negative numbers or shift operations.
How Python Stores Integers in Binary
CPython's int type uses a variable-length array of digits in base 2^30 or 2^15 internally, depending on the build platform. From a developer's perspective, however, you can treat an integer as an infinite sequence of bits in two's complement form. Positive numbers have leading zeros, and negative numbers have leading ones. This abstraction is consistent across all bitwise operations.
For example, the integer 5 is represented as ...000101, while -5 is ...11111011 in two's complement. Python does not expose the infinite leading bits explicitly, but they affect the result of shifts and other bitwise operations.
Reading and Writing Binary Literals
You can write integer literals directly in binary using the 0b prefix. This is useful for defining bit masks or flags without converting from decimal.
mask = 0b11110000
flags = 0b00001111The bin() function returns a string representation with a 0b prefix, and you can use int() with a base to parse binary strings.
value = 0b1010
print(bin(value)) # 0b1010
print(int('1010', 2)) # 10When you print a negative integer with bin(), Python includes a minus sign rather than showing the two's complement bits. For example, bin(-5) returns -0b101. To see the actual two's complement bit pattern, you need to mask the value to a fixed width.
Bitwise Operators and Their Behavior
Python provides six bitwise operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift). These operate on the binary representation of integers.
a = 0b1100
b = 0b1010
print(a & b) # 0b1000 (8)
print(a | b) # 0b1110 (14)
print(a ^ b) # 0b0110 (6)
print(~a) # -13 (inverts all bits)The ~ operator returns the bitwise complement, which in Python is -x - 1 because of the infinite two's complement representation. For example, ~5 is -6. This behavior can surprise developers coming from languages with fixed-width integers.
Two's Complement and Negative Numbers
Python's bitwise operations assume an infinite two's complement representation. This means that shifting right on a negative number preserves the sign bit (arithmetic shift), not a logical shift that fills with zeros.
print(-8 >> 1) # -4
print(-8 >> 2) # -2Left shifts behave as expected, but they can produce large positive numbers because Python integers grow without overflow. For example, 1 << 100 is a valid integer with 101 bits.
To simulate a fixed-width two's complement representation, you can apply a mask after an operation. For instance, to get the 8-bit representation of -1:
value = -1
mask = 0xFF
print(bin(value & mask)) # 0b11111111This is a common pattern when working with hardware registers or network protocols that expect a specific bit width.
Practical Bit Manipulation: Flags and Masks
Bitwise operations are often used to pack multiple boolean flags into a single integer. This reduces memory usage and can improve cache locality when many flags are stored in a list or array.
READ = 0b001
WRITE = 0b010
EXECUTE = 0b100
permission = READ | WRITE # 0b011
if permission & READ:
print("read allowed")To toggle a bit, use XOR with a mask. To clear a bit, use AND with the complement of the mask.
permission ^= WRITE # toggle write
permission &= ~EXECUTE # clear executeThese operations are concise and let you manage several independent flags in a single value.
Performance and Memory Considerations
For small integers, bitwise operations are inexpensive because they are implemented directly in C. However, creating large integers through repeated shifts can allocate memory for the variable-length digit array. For example, 1 << 1000000 creates an integer with more than a million bits. If you need to handle extremely large bit sets, consider using the bitarray library or Python's built-in bytearray for more memory-efficient storage.
For typical flag manipulation, the overhead is negligible. The main performance concern is often the conversion between integers and strings or bytes, not the bitwise operation itself. Use int.from_bytes() and int.to_bytes() when working with binary data from files or network streams.
data = b'\x01\x02'
value = int.from_bytes(data, byteorder='big')
print(value) # 258This approach avoids manual bit shifting for each byte and is more readable.
Common Pitfalls with Bitwise Operations
One frequent mistake is assuming that right shift on a negative number fills with zeros. In Python, it always preserves the sign bit. Another pitfall is using ~ on a positive number and expecting a fixed-width result. For example, ~0b1010 is -11, not 0b0101. If you need a fixed-width bitwise NOT, you must mask the result:
value = 0b1010
width = 4
not_value = (~value) & ((1 << width) - 1)
print(bin(not_value)) # 0b0101Also, be careful when mixing signed and unsigned interpretations. Python has no unsigned integer type, so a value like 0xFFFFFFFF is a positive integer, not -1. If you are porting code from C, convert an N-bit unsigned value to signed by subtracting (1 << N) when the value is at or above 1 << (N - 1).
Finally, remember that the binary bitwise operators &, |, ^, <<, and >> have lower precedence than arithmetic operators but higher than comparisons; ~ is a unary operator and binds more tightly. Use parentheses when mixing expressions to avoid subtle bugs.
# Without parentheses, this is parsed as (a & b) == c, not a & (b == c)
if a & b == c:
passUnderstanding these details helps you write reliable bit manipulation code that behaves predictably across Python versions and platforms.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.