Python bin() function converts an integer into its binary representation. This means it expresses the number using only 0s and 1s, which is how computers fundamentally store and process data. It’s handy when working with the underlying binary structure of numbers.
bin()
Syntax
binary_string = bin(integer)
binary_string
: Variable to store the resulting binary string.bin()
: Built-in Python function for converting to binary.integer
: Integer you want to convert to its binary form.
Example 1: Python bin()
decimal_num = 10
binary_repr = bin(decimal_num)
print(binary_repr)
Code Explanation
- Line 1: Assigns the decimal number 10 to the
decimal_num
variable. - Line 2: Calls the
bin()
function to convert the decimal number to its binary equivalent and stores it in thebinary_repr
variable. - Line 3: Prints the binary representation, which includes the prefix ‘0b’ to indicate it’s a binary number
Output
0b1010
Example 2: Python bin()
with Negative Number
decimal_num = -5
binary_repr = bin(decimal_num)
print(binary_repr)
Code Explanation
- Line 1: Assigns the negative decimal number -5 to the
decimal_num
variable - Line 2:
bin()
handles negative numbers using two’s complement representation - Line 3: Prints the binary representation, including the ‘-‘ sign and the ‘0b’ prefix
Output
-0b101
Example 3: Python bin()
with Large Number
decimal_num = 1024
binary_repr = bin(decimal_num)
print(binary_repr)
Code Explanation
- Line 1: Assigns a larger decimal number to the variable
- Line 2:
bin()
can handle numbers of any size, producing the corresponding binary string - Line 3: Prints the binary representation
Output
0b10000000000
Example 4: Python bin()
with Zero
decimal_num = 0
binary_repr = bin(decimal_num)
print(binary_repr)
Code Explanation
- Line 1: Assigns the value 0 to the variable
- Line 2:
bin()
correctly converts 0 to its binary form - Line 3: Prints the binary representation
Output
0b0
Example 5: Python bin()
with Non-Integer Class
class CustomNumber:
def __init__(self, value):
self.value = value
def __index__(self):
return self.value
custom_num = CustomNumber(12)
binary_num = bin(custom_num)
print(binary_num)
Code Explanation
- Line 1: Defines a custom class to represent numbers.
- Line 2: Initializes an instance with a given
value
. - Line 5: Implements the special
__index__
method, which allows thebin()
function to get an integer representation from the custom object. - Line 8: Creates an instance of
CustomNumber
with the value 12. - Line 9:
bin()
uses the__index__
method to get the integer value and converts it to binary. - Line 10: Prints the binary representation.
Output
0b1100
Example 6: Python bin()
with __index__()
for Non-Integer Class
class CustomNumber:
# ... (same as previous example)
custom_num = CustomNumber(12)
integer_value = custom_num.__index__() # Get the integer value directly
print(integer_value)
Code Explanation
- Line 4: Same as before.
- Line 5: Explicitly calls the
__index__
method to demonstrate howbin()
gets the integer representation. - Line 6: Prints the integer value returned by
__index__
.
Output
12