bin(): display a number in binary
bin(x) converts an integer value into its binary representation. Binary uses only 0 and 1, making it the natural notation for flags, masks, permissions, and low-level data.
Reading binary output
bin(10) produces 0b1010: 8 + 2 equals 10. Each position represents a power of two. The 0b prefix identifies the notation, so the result is not mistaken for a decimal string. For negative values, the displayed representation follows the calculator’s integer rules; confirm the width and signedness required by an external protocol.
Use it for bitwise work
Binary is especially helpful beside bitAnd(), bitOr(), and bitXor(). Convert a mask with bin(), inspect the set positions, then use the appropriate operation. bitNot() is also useful, but its result depends on integer representation.
Input caveats
Supply integers. A fractional value has no simple finite binary integer representation and should be rounded intentionally with floor() or round() only when that is mathematically appropriate. For hexadecimal output, use hex().