Binary, Decimal, and Hex Conversion
Number base conversions are a foundational skill for any developer working close to the hardware layer, writing network code, debugging memory layouts, or working with color values and file permissions. Binary (base-2), hexadecimal (base-16), octal (base-8), and decimal (base-10) all represent the same underlying integer values — just in different notation systems optimized for different human and machine uses. This collection focuses on the values that appear most frequently in systems programming and web development. The byte boundary values (255, 128, 127, 64, 32) are important landmarks: 255 (0xFF) is the maximum value of an unsigned byte, which is why RGB color channels max out at 255. 127 (0x7F) is the maximum positive value of a signed byte. 128 (0x80) sets only the high bit, making it a useful bitmask for checking the sign bit. Unix file permissions in octal are another critical conversion: the chmod 755 that developers type dozens of times a week means exactly 111 101 101 in binary — owner has read+write+execute (7 = 111), group has read+execute (5 = 101), others have read+execute (5 = 101). The octal representation maps neatly to three bits per permission category, which is why Unix uses octal for permissions rather than decimal. Decimal to binary conversion algorithm: repeatedly divide by 2 and record the remainders from bottom to top. For 255: 255/2=127 R1, 127/2=63 R1, 63/2=31 R1, 31/2=15 R1, 15/2=7 R1, 7/2=3 R1, 3/2=1 R1, 1/2=0 R1 → 11111111. Each 1 represents a power of 2 position that's set. Alternatively, hex to binary is a direct substitution: each hex digit maps to exactly four bits (F=1111, 5=0101), making the conversion mechanical once you memorize the 16 hex digit mappings. Hex is used extensively because it provides a compact representation of binary data: two hex digits represent exactly one byte (8 bits). Memory addresses, SHA hashes, cryptographic keys, MAC addresses, and color codes all use hex because the byte-perfect mapping makes them human-readable in a way that raw binary isn't. Bitwise operations in code: understanding binary representation is essential for working with bitwise operators. (flags & 0x04) checks if bit 2 is set. (flags | 0x08) sets bit 3. (flags & ~0x04) clears bit 2. Color manipulation in JavaScript frequently uses these patterns: (color >> 16) & 0xFF extracts the red channel from a 0xRRGGBB hex color. Practical tip: JavaScript's parseInt("ff", 16) converts hex strings to integers, and number.toString(16) converts integers to hex strings. Number.toString(2) gives binary, Number.toString(8) gives octal. These built-in functions handle the conversions correctly for all valid inputs.
# Decimal to Binary 255 = 11111111 (0xFF, 8 bits all set) 128 = 10000000 (0x80, high bit) 127 = 01111111 (0x7F, max signed byte) 64 = 01000000 (0x40) 32 = 00100000 (0x20) # Unix permissions 777 (octal) = 111 111 111 (binary) = rwxrwxrwx 644 (octal) = 110 100 100 (binary) = rw-r--r-- 755 (octal) = 111 101 101 (binary) = rwxr-xr-x
FAQ
- Why do computers use binary instead of decimal?
- Electronic circuits have two stable states: on and off, which map naturally to 1 and 0. Binary arithmetic can be implemented with simple logic gates, making it far more practical than base-10 for hardware.
- Why is hexadecimal used in programming?
- Each hex digit represents exactly 4 bits, so two hex digits represent one byte. This makes hex a compact and readable way to express binary data, memory addresses, and color values.
- How do I convert hex to binary?
- Replace each hex digit with its 4-bit binary equivalent: 0=0000, 1=0001, ..., A=1010, ..., F=1111. For example, 0xFF = 1111 1111 and 0xA3 = 1010 0011.
Related Examples
Hex color codes are the most common way to specify colors in CSS and design tool...
Percentage Calculation ExamplesPercentage calculations are among the most commonly needed arithmetic operations...
Hex Dump and Text-to-Hex EncodingA hex dump shows the hexadecimal byte values of text or binary data alongside it...