ZetCode

Python bin Function

Last modified April 11, 2025

This comprehensive guide explores Python's bin function, which converts integers to their binary string representation. We'll cover basic usage, formatting options, and practical applications in computing.

Basic Definitions

The bin function converts an integer number to a binary string prefixed with "0b". The result is a valid Python expression that could be evaluated back to the original number.

Key characteristics: accepts integers (positive or negative), returns a string, and always includes the "0b" prefix. For non-integers, it raises TypeError.

Basic Integer Conversion

Here's simple usage with different integers showing how bin handles positive, negative, and zero values.

basic_bin.py
# Positive integers
print(bin(5))     # 0b101
print(bin(10))    # 0b1010

# Negative integers
print(bin(-5))    # -0b101
print(bin(-10))   # -0b1010

# Zero
print(bin(0))     # 0b0

This example shows bin with different integer values. Positive numbers get a "0b" prefix, while negatives get "-0b". Zero converts to "0b0".

The binary representation shows the base-2 form of the number. Note that negative numbers are represented with a minus sign, not two's complement.

Working with Different Bases

This example demonstrates how to convert between binary, decimal, and hexadecimal representations using bin, int, and hex.

base_conversion.py
number = 42

# Convert to binary
binary = bin(number)
print(binary)  # 0b101010

# Convert back to decimal
decimal = int(binary, 2)
print(decimal) # 42

# Compare with hexadecimal
print(hex(number)) # 0x2a

The example shows full conversion cycle between decimal and binary. The int function can parse binary strings when given base 2.

This demonstrates Python's consistent base conversion functions: bin for binary, hex for hexadecimal, and int for parsing.

Custom __index__ Method

You can make custom objects work with bin by implementing the __index__ special method. This example creates a custom number.

custom_bin.py
class CustomNumber:
    def __init__(self, value):
        self.value = value
    
    def __index__(self):
        return self.value
    
    def __repr__(self):
        return f"CustomNumber({self.value})"

num = CustomNumber(15)
print(bin(num))  # 0b1111

The CustomNumber class implements __index__ to return its value. When we call bin on an instance, Python uses this method.

This pattern is useful for custom numeric types that should support binary representation while maintaining their own internal state.

Error Handling

The bin function raises TypeError when used with non-integer types. This example shows proper error handling.

errors.py
try:
    print(bin(3.14))
except TypeError as e:
    print(f"Error: {e}")  # 'float' object cannot be interpreted as an integer

class NoIndex:
    pass

try:
    print(bin(NoIndex()))
except TypeError as e:
    print(f"Error: {e}")  # 'NoIndex' object cannot be interpreted as an integer

These examples demonstrate bin's behavior with unsupported types. Floats and objects without __index__ raise TypeError.

To make a class work with bin, implement __index__ as shown in the previous example.

Binary String Manipulation

This example shows practical string manipulation of binary representations, including stripping the prefix and padding with zeros.

string_manipulation.py
number = 42

# Get binary without prefix
binary_str = bin(number)[2:]
print(binary_str)  # '101010'

# Pad with leading zeros
padded = binary_str.zfill(8)
print(padded)      # '00101010'

# Count set bits (1s)
bit_count = binary_str.count('1')
print(bit_count)   # 3

This demonstrates common operations on binary strings. We remove the "0b" prefix, pad with zeros for fixed-width display, and count set bits.

These techniques are useful for bit manipulation, hardware interfacing, and low-level programming tasks where binary representation matters.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.