Binary, Octal and Hexadecimal
Positional bases, weight expansion and fast base-16/base-8 conversions.
- Expand a binary number using positional weights
- Convert between binary, octal and hexadecimal in both directions
- Convert decimal to binary by repeated division
Every processor stores numbers as bits. Hex is just a compact way humans read four bits at a time — 0x3F is the byte 00111111.
Positional notation: a digit's value is digit x base^position, counting positions from 0 at the right. The decimal 345 = 3x100 + 4x10 + 5x1.
Binary (base 2): only 0 and 1. 1011_2 = 1x8 + 0x4 + 1x2 + 1x1 = 11_10.
Octal (base 8) and Hex (base 16): one octal digit = three binary bits; one hex digit = four bits. Convert by regrouping bits from the LSB, padding with leading zeros — never by going through decimal twice.
Decimal to any base: repeated division by the base; read remainders bottom-to-top (LSB first).
Octal groups of 3 and hex groups of 4 both start from the binary LSB. Pad the leftmost group with zeros.
When converting 11011 to octal you must pad: 010 111 — not 11 011.
- 1
Divide by 2 repeatedly
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.
- 2
Read remainders LSB-first
11111111.
- 3
Group into nibbles for hex
1111 1111 = FF in hex.
Solution
255_10 = 11111111_2 = FF_16
Find 174_10 in binary and hexadecimal.
Hint: Divide by 2 seven times; the result fits in one byte.
Reading remainders top-to-bottom
The first remainder is the least significant bit. Reading MSB-first gives a bit-reversed number.
Skipping the leading-zero pad
1001 converted as octal without padding becomes 11 instead of 012.
Convert A3_16 to decimal.
A3 = 10x16 + 3 = 163.
Source: Common university mid-term conversion question.
- Positional weight expansion is the foundation of every base conversion.
- Binary-to-octal = 3-bit groups; binary-to-hex = 4-bit groups from the LSB.
- Decimal-to-base: repeated division; remainders are LSB-first.