CPU Data Representation and Arithmetic Operations
Data Representation on the Central Processing Unit (CPU)
- Data conversion on the CPU involves transforming and manipulating numbers across four primary systems: Decimal, Binary, Octal, and Hexadecimal.
- Arithmetic operations, specifically addition and subtraction, are performed across these different number bases.
- Conversions between these bases are fundamental for processing and understanding data at various levels of computer architecture.
Decimal Number System
- The decimal system is a base-ten system.
- It utilizes ten distinct digits: 0,1,2,3,4,5,6,7,8,9.
- Weights are assigned based on the position of the digit relative to the decimal point (.). The weighting for a decimal number up to three places before and two places after the decimal point is as follows:
- 102=100
- 101=10
- 100=1
- .
- 10−1=0.1
- 10−2=0.01
Binary Number System
- The binary system is a base-two system.
- It utilizes two distinct digits: 0 and 1.
- Weights are assigned relative to the binary point (.). The weighting for a binary number up to three places before and two places after the binary point is as follows:
- 22=4
- 21=2
- 20=1
- .
- 2−1=0.5
- 2−2=0.25
- The Least Significant Bit (LSB) is the rightmost binary digit, representing the lowest weight in a given number.
- The Most Significant Bit (MSB) is the leftmost binary digit, representing the highest weight in a given number.
Octal Number System
- The octal system is a base-eight system.
- It utilizes eight distinct digits: 0,1,2,3,4,5,6,7.
- Weights are assigned relative to the octal point (.). The weighting for an octal number up to three places before and two places after the octal point is as follows:
- 82=64
- 81=8
- 80=1
- .
- 8−1=0.125
- 8−2=0.015625
Hexadecimal Number System
- The hexadecimal system is a base-sixteen system.
- It utilizes sixteen distinct digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
- The letters A through F represent decimal values 10 through 15.
- Weights are assigned relative to the hexadecimal point (.). The weighting for a hexadecimal number up to three places before and two places after the hexadecimal point is as follows:
- 162=256
- 161=16
- 160=1
- .
- 16−1=0.0625
- 16−2=0.0039
One-to-One Comparison of Number Systems
- The following table illustrates the representation of values across the four number systems from decimal 0 to 16:
| Decimal | Binary | Octal | Hexadecimal |
|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 |
| 3 | 11 | 3 | 3 |
| 4 | 100 | 4 | 4 |
| 5 | 101 | 5 | 5 |
| 6 | 110 | 6 | 6 |
| 7 | 111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
Binary Arithmetic
Binary Addition
- There are four basic rules for adding binary digits:
- 0+0=0
- 0+1=1
- 1+0=1
- 1+1=10 (which is 0 carry 1)
- Addition Examples:
- Example 1 (Carries: 1,1,0): 10102+10012=100112
- Example 2 (Carries: 0,1,1): 1012+0112=10002
- Example 3 (Carries: 1,1,1): 11102+10112=110012
- Example 4 (Carries: 1,1,1,1): 101012+110112=1100002
Binary Subtraction
- Key concept: 102=(1×21)+(0×20)=210.
- When subtracting a larger digit from a smaller digit (e.g., 0−1), you must borrow from the next higher position. The borrow carries a value of 2 into the current position.
- Subtraction Examples:
- Example 1: 1102−1012
- Step: Borrow from the second position, making the first position 102. 2−1=1. Result: 0012.
- Verification: 1102=610, 1012=510. 6−5=1.
- Example 2: 110002−1112=100012
- Verification: 110002=(1×24)+(1×23)=16+8=2410. 1112=(1×22)+(1×21)+(1×20)=710. 24−7=1710, which is 100012.
Octal Arithmetic
Octal Addition
- When the sum of two octal digits equals or exceeds 8, a carry is generated for the next higher position.
- Example 1: 3568+1248
- 6+4=1010. Since 10≥8, result is 10−8=2 with a carry of 1.
- 5+2+1(carry)=810. Since 8≥8, result is 8−8=0 with a carry of 1.
- 3+1+1(carry)=5.
- Therefore, 3568+1248=5028.
- Example 2: 10128+1148 could be calculated similarly using base-8 rules.
Octal Subtraction
- If borrowing is required, the borrow from the specific position adds 8 to the current digit.
- Example 1: 2538−1478=1048
- Units: 3<7, so borrow from the tens position. This becomes (8+3)−7=4.
- Tens: Remaining 4−4=0.
- Hundreds: 2−1=1.
- Example 2: 15238−3648
- Units: 3<4, borrow from tens: (8+3)−4=7.
- Tens: Remaining 1<6, borrow from hundreds: (8+1)−6=3.
- Hundreds: Remaining 4−3=1.
- Thousands: 1−0=1.
- Therefore, 15238−3648=11378.
Hexadecimal Arithmetic
Hexadecimal Addition
- Addition follows standard rules, where values exceeding 15 result in a carry-out to the next place value.
- Example 1: 2316+1616=3916.
- Example 2: 2B16+8416
- B(11)+4=15=F.
- 2+8=10=A.
- Result: AF16.
- Example 3: DF16+AC16
- F(15)+C(12)=2710. 27−16=11(B) with carry 1.
- D(13)+A(10)+1(carry)=2410. 24−16=8 with carry 1.
- Result: 18B16.
Hexadecimal Subtraction
- Example 1: 7516−2116=5416.
- Example 2: 8416−2A16
- 4<A(10), borrow 16: (16+4)−10=10=A.
- Remaining 7−2=5.
- Result: 5A16.
Number Base Conversions
Binary to Decimal
- Multiply each binary digit by its weighted position value and sum the results.
- Example 1: Convert 1101012 to decimal.
- (1×25)+(1×24)+(0×23)+(1×22)+(0×21)+(1×20)=32+16+0+4+0+1=5310.
- Example 2: Convert 0.10112 to decimal.
- (1×2−1)+(0×2−2)+(1×2−3)+(1×2−4)=0.5+0+0.125+0.0625=0.687510.
Decimal to Binary
- For integers: Repeatedly divide by 2 and collect the remainders from bottom to top.
- Example 1: Convert 1810 to binary.
- 18/2=9 Remainder 0
- 9/2=4 Remainder 1
- 4/2=2 Remainder 0
- 2/2=1 Remainder 0
- 1/2=0 Remainder 1
- Result: 100102.
- For fractions: Repeatedly multiply by 2. If the result exceeds or equals 1, the carry is 1. Continue with the fractional part until it becomes zero.
- Example 2: Convert 0.312510 to binary.
- 0.3125×2=0.625 (Carry 0)
- 0.625×2=1.250 (Carry 1)
- 0.25×2=0.500 (Carry 0)
- 0.50×2=1.000 (Carry 1)
- Result: 0.01012.
Octal to Decimal
- Multiply each digit by its weighted position value (8n) and sum.
- Example 1: Convert 237.048 to decimal.
- (2×82)+(3×81)+(7×80)+(0×8−1)+(4×8−2)=128+24+7+0+0.0625=159.062510.
Decimal to Octal
- For integers: Repeatedly divide by 8 and collect remainders.
- Example 1: Convert 35910 to octal.
- 359/8=44 Remainder 7
- 44/8=5 Remainder 4
- 5/8=0 Remainder 5
- Result: 5478.
- For fractions: Repeatedly multiply by 8 and collect the carry integers.
- Example 2: Convert 0.312510 to octal.
- 0.3125×8=2.50 (Carry 2)
- 0.5×8=4.00 (Carry 4)
- Result: 0.248.
Binary and Octal Inter-conversion
- Binary to Octal: Break the binary digits into groups of three starting from the LSB. Add leading zeros as MSBs if necessary.
- Octal to Binary: Each octal digit is represented by exactly three binary digits.
- 0:000,1:001,2:010,3:011,4:100,5:101,6:110,7:111.
- Example 1: Convert 138 to binary. 1→001, 3→011. Total: 0010112.
- Example 2: Convert 37.128 to binary. 3→011, 7→111, 1→001, 2→010. Total: 011111.0010102.
Hexadecimal to Decimal
- Multiply each hexadecimal digit by its weighted position (16n).
- Example 1: Convert 35616 to decimal.
- (3×162)+(5×161)+(6×160)=768+80+6=85410.
- Example 2: Convert AF216 to decimal.
- (10×162)+(15×161)+(2×160)=2560+240+2=280210.
Decimal to Hexadecimal
- Repeatedly divide by 16 and record remainders (convert remainders 10–15 to letters A–F).
- Example 1: Convert 65010.
- 650/16=40 Remainder 10(A)
- 40/16=2 Remainder 8
- 2/16=0 Remainder 2
- Result: 28A16.
Hexadecimal and Binary Inter-conversion
- Hexadecimal to Binary: One hexadecimal digit represents four binary digits.
- 0:0000,1:0001,2:0010,3:0011,4:0100,5:0101,6:0110,7:0111,8:1000,9:1001,A:1010,B:1011,C:1100,D:1101,E:1110,F:1111.
- Example 1: Convert 3816 to binary. 3→0011, 8→1000. Total: 001110002.
- Example 2: Convert FB1716 to binary. F(1111),B(1011),1(0001),7(0111). Total: 11111011000101112.
- Binary to Hexadecimal: Group binary digits into sets of four starting from the LSB. Add zeros to the MSB side to complete the last grouping if needed.