Figure 1. Positional notation. Each digit slot carries a power of the base; the value of the number is the sum of digit times place-weight.
Every number system in digital electronics is positional: the digit's value depends on which slot it sits in. The value of a digit string dn−1dn−2…d1d0 in base b is:
N=i=0∑n−1di⋅bi
Change the base and you change the place-weights. The bases below all describe the same physical bits — they just slice them differently for human eyes.
Binary (base 2)
Figure 2. The 8-bit value 10101101₂. Each cell is one bit; weights double every step from the LSB on the right.
Binary uses two symbols, 0 and 1, mapped to the two voltage levels of a digital wire. A single binary digit is a bit; four bits make a nibble; eight bits make a byte.
The 8-bit pattern in Figure 2 evaluates to 128+32+8+4+1=173. An n-bit unsigned register holds values from 0 to 2n−1.
Hexadecimal (base 16)
Figure 3. Hex packs four binary bits into one digit. The same byte 10101101₂ regroups to AD₁₆.
Hex uses sixteen symbols: 0–9 then A–F for ten through fifteen. One hex digit covers exactly four binary bits, so a byte is always two hex digits.
Hex is preferred for memory addresses, register dumps, and machine code because FFFF16=6553510 is faster to read and write than the sixteen-character binary string. The 0x prefix marks a hex literal in C, JavaScript, and most assemblers.
Octal (base 8)
Figure 4. Octal packs three binary bits per digit. The same byte 10101101₂ regroups to 255₈ (with an implicit leading zero in the top group).
Octal uses 0–7 and packs three bits per digit. It mostly survives in Unix file-permission bits (chmod755) and a few legacy systems, where 3-bit groupings are convenient.
Converting between bases
Two methods cover every integer conversion. Weighted sum is best when you're reading a value out of a fixed-base representation; it just adds up di⋅bi. Repeated division is best when you're going into a base; it strips off one digit at a time from the LSB.
Decimal → Binary: repeated division by 2
Figure 5. Convert 173₁₀ to binary by repeatedly dividing by 2. The remainder column, read bottom-up, is the binary value: 10101101₂. Each row peels off one bit, starting from the LSB.
The same algorithm works for any target base. To convert 17310 to octal, divide by 8 instead: remainders 5,5,2 read bottom-up give 2558. To convert to hex, divide by 16: remainders 13,10 read bottom-up give AD16.
Binary → Decimal: weighted sum
Figure 6. Convert 10101101₂ to decimal by summing the weights of every '1' bit. Same operation as Figure 2, written out as a column.
Weighted sum also works directly from hex or octal — the only thing that changes is the base in the place-weights. AD16=10⋅16+13=17310; 2558=2⋅64+5⋅8+5=17310. Same value, three encodings.
Binary ↔ Octal / Hex: bit grouping
Because 8=23 and 16=24, there's a shortcut: group binary bits into 3s for octal or 4s for hex, starting at the radix point. Each group is a single octal/hex digit. Figures 3 and 4 above show the grouping.
Weighted-sum reads a value out of a representation; it sums di⋅bi. Repeated-division writes a value into a representation; remainders bottom-up are the digits. Between binary and octal/hex, bit-grouping beats both — it's a re-slicing of the same bits.
Fractional numbers
Positional notation extends to the right of the radix point with negative powers: d−1⋅b−1+d−2⋅b−2+…. So the binary fraction 0.1012 means 21+81=0.62510.
Figure 7. Convert 0.625₁₀ to binary. Multiply by 2; the integer part of the product is the next bit (top-down — the MSB after the radix point comes first). Stop when the fractional part hits 0.
Integer and fractional parts convert independently. To encode 173.62510 in binary: integer part 17310=101011012 by repeated division; fractional part 0.62510=0.1012 by repeated multiplication; concatenate with the radix point: 10101101.1012.
BCD (Binary-Coded Decimal)
Figure 8. BCD encodes each decimal digit independently in 4 bits. 173₁₀ becomes the three nibbles 0001 0111 0011 — six codes per nibble (1010..1111) are wasted.
BCD treats decimal as the source of truth and stores each digit on its own. It's wasteful — six of every sixteen 4-bit codes are illegal — but the human-readable digits map directly to 7-segment displays and BCD adders.
Note the difference: 17310=101011012 as straight binary, but in BCD the same value is 000101110011BCD. Same number, different encoding.
Gray code
Figure 9. Gray code orders the integers 0..7 so adjacent rows differ by exactly one bit. Compare to the right column where binary 011 → 100 flips three bits at once.
Gray code is a binary encoding where consecutive integers differ by a single bit. The standard formula is g=n⊕(n≫1) — XOR a number with itself shifted right one place.
The single-bit-change property eliminates transient mis-reads in mechanical rotary encoders and is exactly the row/column ordering used inside Karnaugh maps so that adjacent cells differ by one variable.
Two's complement (signed binary)
Figure 10. Number line for 4-bit two's complement: the 16 patterns 0000..1111 split into 0..7 and −8..−1. The MSB is the sign bit — 0 means non-negative, 1 means negative.
Two's complement is the standard way digital hardware represents negative integers. The same bit pattern is read as unsigned (range 0 to 2n−1) or as signed (range −2n−1 to 2n−1−1) — it's the same bits with a different interpretation of the MSB.
To negate a value: invert every bit, then add 1. +310=00112 negates to ∼0011+1=1100+1=11012=−310. The same algorithm round-trips: applying it again to 11012 recovers 00112=+3.
Range table: a 4-bit signed register holds −8..+7; 8-bit holds −128..+127; 16-bit holds −32,768..+32,767; 32-bit holds −231..231−1. The range is asymmetric — there is one more negative value than positive value.
Sign extension
To widen a two's-complement value from n bits to m bits, copy the sign bit (MSB) into every new high bit. So 11012 (4-bit −3) widens to 111111012 (8-bit −3); the original MSB 1 filled the four new high positions. Zero-padding the high bits instead would have given 000011012=+13, which is wrong. Zero-extension only works for unsigned values.
Two's complement is what every ALU's adder assumes. The hardware computes the same XOR/AND/OR sum in both signed and unsigned modes — what changes is just which flag (carry-out for unsigned, overflow for signed) tells you the result is correct. See Adders & Subtractors later in this subject for the gate-level view.
Conversion reference (0..15)
Decimal
Binary
Hex
Octal
Gray
0
0000
0
0
0000
1
0001
1
1
0001
2
0010
2
2
0011
3
0011
3
3
0010
4
0100
4
4
0110
5
0101
5
5
0111
6
0110
6
6
0101
7
0111
7
7
0100
8
1000
8
10
1100
9
1001
9
11
1101
10
1010
A
12
1111
11
1011
B
13
1110
12
1100
C
14
1010
13
1101
D
15
1011
14
1110
E
16
1001
15
1111
F
17
1000
Figure 11. The same sixteen integers across all five common encodings.