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.
# 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
.
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.
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.
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.
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
- Use for readability: Prefer
bin
over manual conversion - Implement __index__: For custom types needing binary representation
- Handle prefix: Remember to strip "0b" when needed
- Consider format: Use f-strings for custom binary formatting
- Document behavior: Clearly document binary representation
Source References
Author
List all Python tutorials.