Progress0 / 7
Topic 03 8 min read
Number Systems & Data Representation
Representation of Data
Binary, octal, hexadecimal and decimal are positional number systems that write the same integer value with different radices. These notes cover place-value conversion, arithmetic in non-decimal bases, BCD and excess-3 codes, the three signed-integer formats, and the character and storage standards — ASCII, EBCDIC, Unicode, bits and bytes — that give bit patterns meaning.
01
Positional notation and place value
- A base-b numeral uses digits 0 to b−1; position i from the right carries weight b^i.
- Decimal 935 = 9×10² + 3×10¹ + 5×10⁰.
- Binary 1011₂ = 8 + 0 + 2 + 1 = 11₁₀; hexadecimal 2F₁₆ = 2×16 + 15 = 47₁₀.
- Fractional digits use negative powers: 0.101₂ = 1/2 + 1/8 = 0.625₁₀.
- Hex digits A–F stand for 10–15; 0x2F, 2F₁₆ and 47₁₀ are the same value.
- The same digits differ by base: 10₂ = 2, 10₈ = 8, 10₁₆ = 16.
Positional notation and place value
Position: 3 2 1 0
Weight 2^: 8 4 2 1
Binary: 1 0 1 1 = 8 + 0 + 2 + 1 = 11
Position: 1 0 -1 -2
Weight 2^: 2 1 1/2 1/4
Binary: 1 0 1 0 = 2 + 0 + 0.5 + 0 = 2.5
Subscript shows the base: 1011_2, 2F_16, 47_1002
Base conversion
- Decimal to base b: divide by b repeatedly; the remainders, read bottom to top, are the digits.
- Base b to decimal: multiply each digit by its place weight b^i and add the products.
- Binary to octal: group 3 bits from the binary point; binary to hex: group 4 bits.
- Pad the first and last groups with zeros; 1001 1100₂ = 9C₁₆ = 234₈.
- Decimal 156 = 1001 1100₂; decimal 45 = 0010 1101₂ = 2D₁₆.
- Check by expanding the result: 234₈ = 2×64 + 3×8 + 4 = 156₁₀.
Decimal, binary, octal, hexadecimal and BCD equivalents
| Decimal | Binary | Octal | Hex | 8421 BCD |
|---|---|---|---|---|
| 0 | 0000 | 0 | 0 | 0000 |
| 1 | 0001 | 1 | 1 | 0001 |
| 5 | 0101 | 5 | 5 | 0101 |
| 8 | 1000 | 10 | 8 | 1000 |
| 10 | 1010 | 12 | A | 0001 0000 |
| 15 | 1111 | 17 | F | 0001 0101 |
| 16 | 1 0000 | 20 | 10 | 0001 0110 |
| 31 | 1 1111 | 37 | 1F | 0011 0001 |
| 156 | 1001 1100 | 234 | 9C | 0001 0101 0110 |
| 255 | 1111 1111 | 377 | FF | 0010 0101 0101 |
Base conversion
156 ÷ 2 = 78 r 0
78 ÷ 2 = 39 r 0
39 ÷ 2 = 19 r 1
19 ÷ 2 = 9 r 1
9 ÷ 2 = 4 r 1
4 ÷ 2 = 2 r 0
2 ÷ 2 = 1 r 0
1 ÷ 2 = 0 r 1 -> read up: 1001 1100
1001 1100 = 9C hex
1001 1100 = 010 011 100 = 234 octal
Expand: 234_8 = 2×64 + 3×8 + 4 = 15603
Binary, octal and hexadecimal arithmetic
- Add column by column and carry 1 when a column sum reaches the base.
- Binary: 1+1 = 10₂ (write 0, carry 1); 1+1+1 = 11₂ (write 1, carry 1).
- Octal: 7+1 = 10₈; a borrow adds 8 to the current column.
- Hex: F+1 = 10₁₆; convert A–F to 10–15 before adding; a borrow adds 16.
- Binary subtraction borrows 2: 1101₂ − 0110₂ = 0111₂ (13 − 6 = 7).
- Binary multiplication is shift-and-add; multiplying by 2 appends a 0.
- Verify every result by converting operands and answer to decimal.
Binary, octal and hexadecimal arithmetic
Binary addition Octal addition Hex addition
1 1 1 carry 1 1 carry 1 carry
1 0 1 1 1 7 2 A
+ 0 1 1 0 + 2 5 + 1 7
--------- ------- -------
1 0 0 0 1 4 4 4 1
(11 + 6 = 17) (15 + 21 = 36) (42 + 23 = 65)
Binary subtraction Octal subtraction Hex subtraction
1 1 0 1 5 2 3 C
- 0 1 1 0 - 2 7 - 1 F
--------- ------- -------
0 1 1 1 2 3 1 D
(13 - 6 = 7) (42 - 23 = 19) (60 - 31 = 29)04
BCD and excess-3
- 8421 BCD stores each decimal digit in a 4-bit nibble with weights 8, 4, 2, 1.
- Patterns 1010–1111 are invalid in BCD; 259₁₀ = 0010 0101 1001.
- BCD addition: if a nibble sum exceeds 9 or a carry leaves the nibble, add 0110 to correct it.
- Example: 8 + 5 gives 1000 + 0101 = 1101; add 0110 → 1 0011, i.e. 13.
- Excess-3 adds 0011 to each digit’s BCD code: 0 → 0011, 4 → 0111, 9 → 1100.
- Excess-3 is self-complementing: inverting its bits gives the code for 9 − d.
- BCD uses 4 bits per digit, so it is not the compact binary encoding of the integer.
Decimal digits in 8421 BCD and excess-3
| Decimal | 8421 BCD | Excess-3 |
|---|---|---|
| 0 | 0000 | 0011 |
| 1 | 0001 | 0100 |
| 2 | 0010 | 0101 |
| 3 | 0011 | 0110 |
| 4 | 0100 | 0111 |
| 5 | 0101 | 1000 |
| 6 | 0110 | 1001 |
| 7 | 0111 | 1010 |
| 8 | 1000 | 1011 |
| 9 | 1001 | 1100 |
BCD and excess-3
259 in 8421 BCD: 0010 0101 1001
407 in excess-3: 4 -> 0111, 0 -> 0011, 7 -> 1010
=> 0111 0011 1010
BCD addition 8 + 5:
1000
+ 0101
------
1101 invalid BCD digit (> 1001)
+ 0110 add correction
------
1 0011 = 13 (carry 1, digit 3)
Excess-3 self-complement:
4 = 0111 -> invert -> 1000 = excess-3 for 5, and 9 - 4 = 505
Signed numbers: sign-magnitude, 1’s and 2’s complement
- Unsigned n bits span 0 to 2ⁿ−1; 8 bits span 0–255.
- Sign-magnitude: leftmost bit is the sign (0 = +, 1 = −), remaining bits are magnitude; two zeros.
- 1’s complement: invert every bit of the magnitude; two zeros; addition needs end-around carry.
- 2’s complement: invert every bit and add 1; one zero; subtract by adding the 2’s complement.
- The 2’s complement sign bit has weight −2ⁿ⁻¹: 1000 0000 = −128 in 8 bits.
- Ranges: sign-magnitude and 1’s complement −(2ⁿ⁻¹−1) to +(2ⁿ⁻¹−1); 2’s complement −2ⁿ⁻¹ to 2ⁿ⁻¹−1.
- Form −45: +45 = 0010 1101 → invert = 1101 0010 → add 1 = 1101 0011.
8-bit signed representations
| Value | Sign-magnitude | 1’s complement | 2’s complement |
|---|---|---|---|
| +5 | 0000 0101 | 0000 0101 | 0000 0101 |
| −5 | 1000 0101 | 1111 1010 | 1111 1011 |
| +0 | 0000 0000 | 0000 0000 | 0000 0000 |
| −0 | 1000 0000 | 1111 1111 | none (0000 0000 = +0) |
| +127 | 0111 1111 | 0111 1111 | 0111 1111 |
| −127 | 1111 1111 | 1000 0000 | 1000 0001 |
| −128 | not representable | not representable | 1000 0000 |
Signed numbers: sign-magnitude, 1’s and 2’s complement
+45 = 0010 1101
1's comp = 1101 0010
2's comp = 1101 0011 = -45
Check: -128 + 64 + 16 + 2 + 1 = -45
Unsigned reading of 1101 0011 = 21106
Characters, bits, bytes and word size
- 1 bit = one binary digit; 1 nibble = 4 bits; 1 byte = 8 bits.
- ASCII is a 7-bit code for 128 characters: A = 65, a = 97, 0 = 48.
- EBCDIC is an 8-bit IBM mainframe code; its letters are not contiguous.
- Unicode assigns a code point to every character; UTF-8 encodes 1–4 bytes and matches ASCII below 128.
- Word size is the number of bits a CPU handles at once, e.g. 8, 16, 32 or 64.
- An n-bit field has 2ⁿ patterns: 8 bits = 256, 16 bits = 65,536.
- Storage: 1 KiB = 1024 B, 1 MiB = 1024 KiB, 1 GiB = 1024 MiB.
Character codes and storage units
| Code / unit | Size | Example |
|---|---|---|
| ASCII | 7 bits, 128 codes | A = 65 = 100 0001 |
| EBCDIC | 8 bits, 256 codes | A = 193 = 1100 0001 |
| Unicode | code points to U+10FFFF | U+0041 = A |
| UTF-8 | 1–4 bytes per code point | A = 41₁₆ (1 byte) |
| Byte | 8 bits | 256 values |
| Word | CPU register width | 32 or 64 bits |
Exam points
- Binary 1011 0110₂ = B6₁₆ = 266₈ = 182₁₀.
- Decimal 156 = 1001 1100₂ = 234₈ = 9C₁₆.
- 8421 BCD for 259 is 0010 0101 1001; excess-3 for 4 is 0111.
- In 8-bit 2’s complement, −45 = 1101 0011 and the range is −128 to +127.
- The 1’s complement of 0000 0101 is 1111 1010; adding 1 gives the 2’s complement 1111 1011.
- A byte is 8 bits; ASCII A = 65, a = 97, 0 = 48; UTF-8 is ASCII-compatible.
Key terms
- Radix
- The base of a positional number system; it fixes the digit set 0 to b−1 and the place weights b^i.
- Two’s complement
- An n-bit signed encoding in which a negative value is the bitwise inverse of its magnitude plus 1; its range is −2ⁿ⁻¹ to 2ⁿ⁻¹−1 and it has one zero.
- BCD
- Binary-Coded Decimal: each decimal digit is stored as a 4-bit 8421 code, and the six patterns 1010–1111 are unused.
Check yourself
Quick check
01
What is 2A₁₆ + 17₁₆?
02
Which 8-bit pattern represents −5 in 2’s complement?