Python
Programming
Binary Conversion
Data Types
Coding Techniques

Convert an int to a binary string in Python

Codemia Knowledge Hub ·

Use bin(n) to convert an integer to a binary string in Python; it returns a string like '0b101101'. For output without the 0b prefix, use format(n, 'b') or the f-string f'{n:b}'. For fixed width with leading zeros, add a width specifier such as f'{n:08b}'.

bin(), format() and f-strings

All three built-ins produce the binary representation; they differ only in formatting.

python
1number = 45
2
3# bin() includes the 0b prefix
4print(bin(number))          # '0b101101'
5
6# format() returns clean digits
7print(format(number, 'b'))  # '101101'
8
9# f-string equivalent
10print(f'{number:b}')        # '101101'
MethodOutputBest For
bin(n)'0b101101'Debugging, REPL inspection, when prefix is expected
format(n, 'b')'101101'String building, logging, display
f'{n:b}''101101'Inline string formatting

All three handle arbitrarily large integers, because Python integers have no fixed bit width.

Remember that bin() includes the prefix: bin(45) == '101101' is False because the value is '0b101101'. Use format(n, 'b') when you need bare digits.

Fixed width with leading zeros

Byte values, bit masks and protocol fields usually need a fixed number of digits:

python
1value = 13
2
3# 8-bit representation
4print(f'{value:08b}')       # '00001101'
5
6# 16-bit representation
7print(f'{value:016b}')      # '0000000000001101'
8
9# 32-bit representation
10print(f'{value:032b}')      # '00000000000000000000000000001101'

08b means pad with zeros to 8 characters and format as binary. It is clearer than prepending zeros with zfill():

python
1# Works, but less clear than the format specifier
2print(bin(13)[2:].zfill(8))  # '00001101'
3
4# Preferred: the intent is obvious
5print(f'{13:08b}')           # '00001101'

Without a width, format(5, 'b') returns '101', which is correct but hard to read when you expect 8-bit alignment. Use '08b' or similar whenever bit width matters.

Negative numbers and two's complement

Because Python integers are arbitrary precision, there is no fixed bit width, and bin(-5) returns the sign plus the magnitude rather than a two's complement pattern:

python
print(bin(-5))          # '-0b101'
print(format(-5, 'b'))  # '-101'

Getting a two's complement string

To get a fixed-width two's complement representation (how hardware stores negative integers), mask the value to the desired width:

python
1value = -5
2
3# 8-bit two's complement
4as_byte = value & 0xFF
5print(f'{as_byte:08b}')    # '11111011'
6
7# 16-bit two's complement
8as_word = value & 0xFFFF
9print(f'{as_word:016b}')   # '1111111111111011'
10
11# 32-bit two's complement
12as_dword = value & 0xFFFFFFFF
13print(f'{as_dword:032b}')  # '11111111111111111111111111111011'

The bitwise AND with a mask (0xFF for 8 bits, 0xFFFF for 16 bits) forces the two's complement representation at that width.

Converting two's complement back to signed

python
1def twos_complement_to_int(binary_str: str) -> int:
2    """Convert a two's complement binary string to a signed integer."""
3    width = len(binary_str)
4    value = int(binary_str, 2)
5    if value >= (1 << (width - 1)):
6        value -= (1 << width)
7    return value
8
9print(twos_complement_to_int('11111011'))  # -5
10print(twos_complement_to_int('00001101'))  #  13

Converting a binary string back to an int

int() parses binary strings:

python
1# With 0b prefix
2print(int('0b101101', 2))   # 45
3
4# Without prefix
5print(int('101101', 2))     # 45
6
7# With leading zeros
8print(int('00101101', 2))   # 45
9
10# Using int() with base 0 auto-detects prefix
11print(int('0b101101', 0))   # 45

The second argument is the base: 2 for binary, 8 for octal, 16 for hexadecimal. The result of f'{13:08b}' is the string '00001101', not a number, so convert it back with int('00001101', 2) before doing arithmetic.

Manual conversion

The built-ins are well-tested and cover standard use cases, so keep manual implementations for learning how the conversion works.

Repeated division

Repeated division builds the string from least significant to most significant bit:

python
1def int_to_binary(n: int) -> str:
2    """Convert a non-negative integer to binary using repeated division."""
3    if n == 0:
4        return '0'
5
6    bits = []
7    value = n
8    while value > 0:
9        bits.append(str(value % 2))
10        value //= 2
11
12    return ''.join(reversed(bits))
13
14print(int_to_binary(45))   # '101101'
15print(int_to_binary(0))    # '0'
16print(int_to_binary(255))  # '11111111'

Each iteration takes the remainder of division by 2 (the current least significant bit), then integer-divides by 2 to shift right. The bits are collected in reverse and reversed at the end.

Bit shifts and masks

python
1def int_to_binary_bitwise(n: int) -> str:
2    """Convert a non-negative integer to binary using bit shifts and masks."""
3    if n == 0:
4        return '0'
5
6    bits = []
7    value = n
8    while value:
9        bits.append(str(value & 1))  # Extract lowest bit
10        value >>= 1                  # Shift right by 1
11
12    return ''.join(reversed(bits))
13
14print(int_to_binary_bitwise(45))   # '101101'

This is logically identical to the division method, using & 1 instead of % 2 and >>= 1 instead of //= 2. Many developers find it more intuitive when thinking in binary.

Practical uses

Displaying bit flags

python
1# Unix file permissions
2permission = 0o755
3print(f'rwx pattern: {permission:09b}')  # '111101101'
4
5# Feature flags
6flags = 0b1010
7features = ['logging', 'metrics', 'tracing', 'alerts']
8for i, feature in enumerate(features):
9    enabled = bool(flags & (1 << (len(features) - 1 - i)))
10    print(f'{feature}: {"on" if enabled else "off"}')

Network subnet masks

python
1def display_subnet(mask: int) -> str:
2    """Display an IPv4 subnet mask in both dotted-decimal and binary."""
3    octets = [(mask >> (8 * i)) & 0xFF for i in range(3, -1, -1)]
4    decimal = '.'.join(str(o) for o in octets)
5    binary = '.'.join(f'{o:08b}' for o in octets)
6    return f'{decimal}  ({binary})'
7
8print(display_subnet(0xFFFFFF00))
9# 255.255.255.0  (11111111.11111111.11111111.00000000)
10
11print(display_subnet(0xFFFFF000))
12# 255.255.240.0  (11111111.11111111.11110000.00000000)

Inspecting bytes

python
1data = b'Hi'
2for byte in data:
3    print(f'{byte:#010b}  ({chr(byte)})')
4# 0b01001000  (H)
5# 0b01101001  (i)

The # flag adds the 0b prefix, and 010 pads to 10 characters total, including the 0b.

Binary, octal and hex format specifiers

python
1n = 255
2
3print(f'Binary:      {n:b}')     # '11111111'
4print(f'Octal:       {n:o}')     # '377'
5print(f'Decimal:     {n:d}')     # '255'
6print(f'Hexadecimal: {n:x}')     # 'ff'
7print(f'Hex (upper): {n:X}')     # 'FF'
8
9# With prefixes
10print(f'Binary:      {n:#b}')    # '0b11111111'
11print(f'Octal:       {n:#o}')    # '0o377'
12print(f'Hex:         {n:#x}')    # '0xff'

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms