You are inspecting the low-level representation of permissions, bit masks or identifiers.
What You Need to Understand
bin(integer) returns a string representation of an integer in base 2 (binary). Its usual output includes the 0b prefix so the base is visible. It does not change the integer itself.
When to Use It
You are inspecting the low-level representation of permissions, bit masks or identifiers.
Step-by-Step Python Example
number = 42
print(bin(number))
print(bin(-number))
print(type(bin(number)).__name__)
How the Example Works
The function returns text, including a sign for a negative value. The last line prints str, showing that the result is not an integer.
Example Output
0b101010
-0b101010
str
101010
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
number = 42
formatted = format(number, "b")
print(formatted)
Important Behavior and Edge Cases
Use int(text, base) when converting a formatted representation back to a number. Leading prefixes are not regular digits in the corresponding base.
Try It Yourself
Convert the integer 64 and explain the prefix in the output.
Common Beginner Mistakes
- Use
int(text, base)when converting a formatted representation back to a number. Leading prefixes are not regular digits in the corresponding base. - Applying a string method before defining whether text is intended for display, comparison, parsing, or input validation.
Key Takeaways
bin(integer)returns a string representation of an integer in base 2 (binary).- The function returns text, including a sign for a negative value.
- Use
int(text, base)when converting a formatted representation back to a number.