How do you express binary literals in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python supports binary integer literals directly. You write them with a 0b prefix, and Python stores the result as a normal int. This is useful when code deals with flags, masks, packed bytes, protocols, and bitwise operations.
The Syntax for Binary Literals
A binary literal starts with 0b or 0B, followed by digits 0 and 1 only.
All of these values are ordinary Python integers. The fact that a value was written in binary is source-code notation, not a separate runtime type.
Make Long Bit Patterns Readable
Underscores are allowed inside numeric literals for readability. This is especially useful when a value represents grouped fields or byte boundaries.
The underscores are ignored by the parser. They exist only to make the source code easier to review.
Convert Between Text and Binary Integers
If you have binary digits in a string, use int(text, 2). To convert an integer back to a binary representation, use bin or format.
bin includes the 0b prefix. format(value, "08b") is often more useful when you want fixed-width output.
Use Binary Literals With Bitwise Operations
Binary literals become most valuable when combined with bitwise logic because they show intent directly.
Without binary notation, the same code would work with decimal values, but the meaning of each bit would be much harder to see at a glance.
Practical Example: Parsing a Status Byte
Suppose a device gives you one status byte where each bit means something different.
This style maps neatly to documentation tables that describe individual bits. Reviewers can compare the code to the protocol spec without mentally converting every constant from decimal.
Binary Values and Bytes
Binary literals are also useful when converting to or from raw bytes.
This pattern appears in binary formats, network protocols, and test fixtures for low-level code.
Python integers are arbitrary precision, which is convenient but also means they do not automatically behave like eight-bit or sixteen-bit machine values. If width matters, mask explicitly.
Common Pitfalls
A common mistake is confusing binary text with a binary literal. "1010" is a string. 0b1010 is an integer.
Another mistake is expecting bin to produce fixed-width output. It does not pad with leading zeros. Use format(value, "08b") or a similar width when layout matters.
It is also easy to forget that Python integers are not fixed-width machine registers. If you need byte-sized overflow behavior, apply a mask explicitly.
Finally, invalid digits such as 0b1021 are syntax errors because only 0 and 1 are allowed after the prefix.
Summary
- Use
0bor0Bto write binary literals in Python. - Binary literals are ordinary
intvalues at runtime. - Underscores improve readability for longer bit patterns.
- '
int(text, 2),bin, andformathandle most conversions.' - Binary notation is especially useful for flags, masks, and byte-level code.
Related reading
- How do you generate dynamic parameterized unit tests in Python?
- How do you get a directory listing sorted by creation date in python?
- How do you get the logical xor of two variables in Python?
- How do you get the logical xor of two variables in Python?
- How do you get the magnitude of a vector in Numpy?
- How do you get tkinter to work with asyncio?
- How do you install modules within sagemaker training jobs?
- How do you locally load model.tar.gz file from Sagemaker?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.