bin()

Convert an integer to its binary literal form — a string, not a number.

Built-in functionPython 2.6+Live demo
Common call
bin(255) # "0b11111111"
Returns
a str with "0b" prefix
Replaces
format(n, "b") when you also want the prefix
Watch out
floats are rejected — use int() first
bin(xxAny integer (positive, negative, or zero). Objects with __index__ also accepted. Floats raise TypeError.type: int · required)
str

Demo

Live evaluation
Try:
Inputs
xintthe integer
Output
bin(10)
'0b1010'

bin returns a STRING in the same form you would type a binary integer literal: "0b" prefix, digits 0 and 1. Negatives get a leading "-". This is a formatter, not a math operation — the value does not change, only its representation.

Parameters

NameTypeRequiredDescription
xintyesAny integer (positive, negative, or zero). Objects with __index__ also accepted. Floats raise TypeError.

Return value

strA string in Python integer literal form: "0b" prefix, digits 0 and 1. Negatives get a "-0b" prefix.

Common patterns

Count set bits
Bit-count via string count works, though int.bit_count() is faster in 3.10+.
ones = bin(n).count("1")
# or, 3.10+:
ones = n.bit_count()
Show a bit pattern
Debug-friendly display of masks and flags.
print(f"flags = {bin(flags)}")
Round-trip via int
bin → str, int with base=2 → back. The prefix is optional on parse.
s = bin(10)         # "0b1010"
n = int(s, 2)       # 10

Examples

1. Small integer
bin(10)
Returns
"0b1010"
2. Zero
bin(0)
Returns
"0b0"
3. One
bin(1)
Returns
"0b1"
4. Negative
bin(-10)
Returns
"-0b1010"
5. Byte max
bin(255)
Returns
"0b11111111"
6. Float raises
bin(1.5)
Returns
TypeError: 'float' object cannot be interpreted as an integer

Pitfalls

1. The "0b" prefix is included
Great for humans reading Python literals; in the way when you want a fixed-width bit pattern.
Prefix included
bin(5)
"0b101"
Fixed-width without prefix
format(5, "08b")
"00000101"
2. Floats are rejected
bin only accepts integers. Truncate or round first.
Type error
bin(1.5)
TypeError: 'float' object cannot be interpreted as an integer
Truncate first
bin(int(1.5))
"0b1"
3. Negatives are sign-magnitude, not two's complement
bin(-10) is "-0b1010", not a two's complement fixed-width form. Python integers are unbounded — there is no fixed width to complement against.
Not two's comp
bin(-10)
"-0b1010"
Mask to a width
format(-10 & 0xFF, "08b")
"11110110" # 8-bit two's complement
4. Result is a str, not a number
You cannot do arithmetic on the binary string. Parse it back if needed.
Type error
bin(3) + bin(5)
"0b110b101" # string concat, not add
Add first
bin(3 + 5)
"0b1000"

When to use

Use it
  • Human-readable display of small integers as bit patterns
  • Debugging masks, flags, and bitwise operations
  • Round-trippable output that int(s, 2) can parse back
Reach for something else
  • Fixed-width bit pattern → format(n, "0Nb")
  • No prefix → format(n, "b")
  • Two's complement view → mask first (n & 0xFF)
  • Bit count → int.bit_count() (3.10+) is faster than counting the string

Notes

Complexity
O(log n) in the number of digits
Return
str — always with "0b" (or "-0b") prefix
CPython impl
Python/bltinmodule.c :: builtin_bin — delegates to __index__ then formats
Memory
Allocates one small string
Thread-safe
Yes — a pure computation

FAQ

Use format() or an f-string with the "b" spec — cleaner and lets you pad.

format(5, "b")       # "101"
format(5, "08b")     # "00000101"
f"{5:08b}"           # "00000101"

History

2.6
bin() introduced — same version as the "0b" literal syntax.