Loading lessons...
Number Systems, Base Conversion & Binary Arithmetic
Number Systems, Base Conversion & Binary Arithmetic
A digital computer stores and processes everything as patterns of two voltage levels, written 0 and 1. To reason about these patterns we need number systems other than decimal: binary (base 2) for the hardware itself, and octal (base 8) and hexadecimal (base 16) as short ways of writing long binary strings. This lesson covers positional notation, every conversion asked in IPU papers, and arithmetic in these bases.
1. Positional Number Systems
In a positional (weighted) number system with base or radix r:
- there are exactly r digits: 0, 1, ldots, r - 1;
- each position has a weight that is a power of r;
- the value is the sum of each digit times its weight.
A number with n integer digits and m fraction digits
has the decimal value
Positional weights of a binary number
| System | Base r | Digits | Example |
|---|---|---|---|
| Binary | 2 | 0, 1 | (1101.101)2 |
| Octal | 8 | 0 to 7 | (725.3)8 |
| Decimal | 10 | 0 to 9 | (469.375)10 |
| Hexadecimal | 16 | 0 to 9, A to F | (3A7.C)16 |
The leftmost bit of a binary number is the most significant bit (MSB); the rightmost is the least significant bit (LSB). A group of 4 bits is a nibble and 8 bits is a byte.
The first 16 numbers in each base
| Decimal | Binary | Octal | Hex | Decimal | Binary | Octal | Hex |
|---|---|---|---|---|---|---|---|
| 0 | 0000 | 0 | 0 | 8 | 1000 | 10 | 8 |
| 1 | 0001 | 1 | 1 | 9 | 1001 | 11 | 9 |
| 2 | 0010 | 2 | 2 | 10 | 1010 | 12 | A |
| 3 | 0011 | 3 | 3 | 11 | 1011 | 13 | B |
| 4 | 0100 | 4 | 4 | 12 | 1100 | 14 | C |
| 5 | 0101 | 5 | 5 | 13 | 1101 | 15 | D |
| 6 | 0110 | 6 | 6 | 14 | 1110 | 16 | E |
| 7 | 0111 | 7 | 7 | 15 | 1111 | 17 | F |
Learn this table by heart. Every octal and hex conversion in the exam is just a lookup in it.
2. Any Base to Decimal
Multiply each digit by its weight and add (the formula above).
Convert (1101.101)2, (725.3)8 and (3A7.C)16 to decimal.
- (1101.101)2 = 1 · 8 + 1 · 4 + 0 · 2 + 1 · 1 + 1 · tfrac{1}{2} + 0 · tfrac{1}{4} + 1 · tfrac{1}{8} = 13.625
- (725.3)8 = 7 · 64 + 2 · 8 + 5 + 3 · tfrac{1}{8} = 448 + 16 + 5 + 0.375 = 469.375
- (3A7.C)16 = 3 · 256 + 10 · 16 + 7 + 12 · tfrac{1}{16} = 768 + 160 + 7 + 0.75 = 935.75
The same rule works for any base, for example (245)6 = 2 · 36 + 4 · 6 + 5 = (101)10.
3. Decimal to Any Base
The integer part and the fraction part are converted separately.
- Integer part: repeated division by r. Divide by r, write down the remainder, and keep dividing the quotient until it becomes 0. The remainders read bottom to top (last remainder first) give the digits.
- Fraction part: repeated multiplication by r. Multiply the fraction by r; the integer part of the product is the next digit. Continue with the new fraction until it becomes 0 or enough digits are found. The digits read top to bottom.
Repeated division and multiplication for 156.6875
Convert (156.6875)10 to binary, octal and hexadecimal.
Integer 156 to binary: 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. Reading upward: 10011100.
Fraction 0.6875 to binary: 0.6875 × 2 = 1.375, 0.375 × 2 = 0.75, 0.75 × 2 = 1.5, 0.5 × 2 = 1.0. Reading downward: .1011.
Octal: 156/8 = 19 r 4, 19/8 = 2 r 3, 2/8 = 0 r 2, so 234; 0.6875 × 8 = 5.5, 0.5 × 8 = 4.0, so .54.
Hex: 156/16 = 9 r 12 (C), 9/16 = 0 r 9, so 9C; 0.6875 × 16 = 11.0, so .B.
Fractions that never end
A decimal fraction can be exact in base 10 and endless in base 2. Converting 0.1:
| Step | Fraction × 2 | Bit |
|---|---|---|
| 1 | 0.2 | 0 |
| 2 | 0.4 | 0 |
| 3 | 0.8 | 0 |
| 4 | 1.6 | 1 |
| 5 | 1.2 | 1 |
| 6 | 0.4 | 0 |
| 7 | 0.8 | 0 |
| 8 | 1.6 | 1 |
The pattern 0011 repeats forever: (0.1)10 = (0.0overline{0011})2 = 0.000110011ldots This is why 0.1 + 0.2 is not exactly 0.3 in floating point: the stored value is rounded. In an exam, stop after the number of places asked (usually 4 to 6) and say "approximately".
4. Binary, Octal and Hex Shortcuts
Because 8 = 23 and 16 = 24, each octal digit is exactly 3 bits and each hex digit is exactly 4 bits. No arithmetic is needed:
- Binary to octal / hex: starting at the binary point, group bits in 3s (octal) or 4s (hex), going left for the integer part and right for the fraction. Pad the ends with zeros and replace each group by its digit.
- Octal / hex to binary: replace every digit by its 3-bit or 4-bit pattern.
- Octal to hex (or back): go through binary.
Convert (1011011110.01101)2 to octal and hexadecimal, and (5736)8 to hexadecimal.
- Octal groups: 001 011 011 110 . 011 010 ⇒ (1336.32)8
- Hex groups: 0010 1101 1110 . 0110 1000 ⇒ (2DE.68)16
- (5736)8 = 101 111 011 110 = 1011 1101 1110 = (BDE)16
Always group outward from the binary point. Grouping the fraction from the right end gives a wrong answer: .01101 must become .0110 1000, not .0000 1101.
5. Finding an Unknown Base
Find the base b if (121)b = (144)8.
- Right side: (144)8 = 64 + 32 + 4 = 100.
- Left side: (121)b = b2 + 2b + 1 = (b + 1)2.
- (b + 1)2 = 100 gives b + 1 = 10, so b = 9 (the negative root is not a base).
- Check: digit 2 is allowed in base 9.
6. Binary Arithmetic
Addition
| A | B | Carry-in | Sum | Carry-out |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
In short: 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1).
Subtraction
0 - 0 = 0, 1 - 0 = 1, 1 - 1 = 0, and 0 - 1 = 1 with a borrow of 1 from the next column (because 10 - 1 = 1). Computers do not subtract this way; they add the complement (next lesson).
Multiplication and division
Binary multiplication is shift-and-add: each bit of the multiplier is 0 (add nothing) or 1 (add the multiplicand, shifted). Division is shift-and-subtract, and each quotient bit is 1 if the divisor fits, else 0.
Compute 10110 + 01101, 11010 - 01101, 1011 × 101 and 110111 ÷ 101.
- Add: 10110 + 01101 = 100011 (check: 22 + 13 = 35).
- Subtract: 11010 - 01101 = 01101 (check: 26 - 13 = 13).
- Multiply: partial products 1011, 0000 (shifted 1), 1011 (shifted 2); sum = 110111 (check: 11 × 5 = 55).
- Divide: 110111 ÷ 101 = 1011, remainder 0 (check: 55 ÷ 5 = 11).
The multiplication laid out in columns:
1 0 1 1 multiplicand (11)
x 1 0 1 multiplier (5)
---------
1 0 1 1 bit 0 = 1: add multiplicand
0 0 0 0 bit 1 = 0: add zero, shifted left 1
1 0 1 1 bit 2 = 1: add multiplicand, shifted left 2
-----------
1 1 0 1 1 1 product (55)Octal and hexadecimal addition
Add digit by digit in decimal. If the column sum reaches the base, subtract the base and carry 1.
Add (3AF)16 + (1C9)16 and (765)8 + (243)8.
- Hex, right column: F + 9 = 15 + 9 = 24 = 16 + 8, write 8, carry 1.
- Middle: A + C + 1 = 10 + 12 + 1 = 23 = 16 + 7, write 7, carry 1.
- Left: 3 + 1 + 1 = 5. Result (578)16 (check: 943 + 457 = 1400).
- Octal: 5 + 3 = 8 = 8 + 0, write 0 carry 1; 6 + 4 + 1 = 11 = 8 + 3, write 3 carry 1; 7 + 2 + 1 = 10 = 8 + 2, write 2 carry 1. Result (1230)8 (check: 501 + 163 = 664).
7. Range of n Bits
With n bits there are 2n different patterns, so an unsigned n-bit number covers 0 to 2n - 1:
| Bits | Patterns | Unsigned range |
|---|---|---|
| 4 | 16 | 0 to 15 |
| 8 | 256 | 0 to 255 |
| 10 | 1024 | 0 to 1023 |
| 16 | 65,536 | 0 to 65,535 |
| 32 | about 4.29 × 109 | 0 to 4,294,967,295 |
To find how many bits a decimal number N needs, use n = ⌈ log2 (N + 1) ⌉. For example 2023 needs 11 bits, since 210 = 1024 ≤ 2023 < 2048 = 211: (2023)10 = (11111100111)2 = (3747)8 = (7E7)16.
Key points to remember
- Value = ∑ ai ri; this converts any base to decimal.
- Decimal to base r: integer by repeated division (remainders read upward), fraction by repeated multiplication (integer parts read downward).
- Octal digit = 3 bits, hex digit = 4 bits; group outward from the point.
- Some decimal fractions such as 0.1 never end in binary.
- n bits give 2n patterns, unsigned range 0 to 2n - 1.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Convert (156.6875)10 into binary, octal and hexadecimal. Show all steps. (5 Marks)
- Write: the division ladder for 156, the multiplication ladder for 0.6875, then octal and hex either directly or by grouping the binary result.
- Q2. Convert (3A7.C)16 to decimal and octal. (2.5 Marks)
- Write: the weighted sum (935.75), then hex to binary to octal: 0011 1010 0111.1100 = 1647.68.
- Q3. Find the base b if (121)b = (144)8. (2.5 Marks)
- Q4. Perform in binary: 1011 × 1101 and 110111 ÷ 101. Verify in decimal. (5 Marks)
- Write: 1011 × 1101 = 10001111 (11 × 13 = 143) with all partial products, and the long division.
- Q5. Why can (0.1)10 not be represented exactly in binary? (2.5 Marks)