Python
Binary Literals
Programming
Code Syntax
Python Tips

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.

Browse interview questions

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.

python
1a = 0b0
2b = 0b1010
3c = 0B1111
4
5print(a)
6print(b)
7print(c)
8print(type(b))

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.

python
1mask = 0b1111_0000
2flag = 0b0000_0100
3mode = 0b1010_1100_0001_0011
4
5print(mask)
6print(flag)
7print(mode)

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.

python
1text = "101101"
2value = int(text, 2)
3
4print(value)
5print(bin(value))
6print(format(value, "08b"))

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.

python
1READ = 0b0001
2WRITE = 0b0010
3EXECUTE = 0b0100
4
5permissions = READ | WRITE
6print(format(permissions, "04b"))
7
8print(bool(permissions & READ))
9print(bool(permissions & EXECUTE))
10
11permissions ^= WRITE
12print(format(permissions, "04b"))

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.

python
1STATUS_READY = 0b0000_0001
2STATUS_ERROR = 0b0000_0010
3STATUS_OVERHEAT = 0b0000_0100
4STATUS_REMOTE = 0b0000_1000
5
6status = 0b0000_1101
7
8print("ready", bool(status & STATUS_READY))
9print("error", bool(status & STATUS_ERROR))
10print("overheat", bool(status & STATUS_OVERHEAT))
11print("remote", bool(status & STATUS_REMOTE))

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.

python
1value = 0b1100_1010
2payload = value.to_bytes(1, byteorder="big", signed=False)
3
4print(payload)
5print(payload.hex())
6
7round_trip = int.from_bytes(payload, byteorder="big", signed=False)
8print(round_trip == value)

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.

python
value = 0b1_1111_1111
print(format(value & 0xFF, "08b"))

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 0b or 0B to write binary literals in Python.
  • Binary literals are ordinary int values at runtime.
  • Underscores improve readability for longer bit patterns.
  • 'int(text, 2), bin, and format handle most conversions.'
  • Binary notation is especially useful for flags, masks, and byte-level code.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.