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,90, 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=10010^2 = 100
    • 101=1010^1 = 10
    • 100=110^0 = 1
    • ..
    • 101=0.110^{-1} = 0.1
    • 102=0.0110^{-2} = 0.01

Binary Number System

  • The binary system is a base-two system.
  • It utilizes two distinct digits: 00 and 11.
  • 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=42^2 = 4
    • 21=22^1 = 2
    • 20=12^0 = 1
    • ..
    • 21=0.52^{-1} = 0.5
    • 22=0.252^{-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,70, 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=648^2 = 64
    • 81=88^1 = 8
    • 80=18^0 = 1
    • ..
    • 81=0.1258^{-1} = 0.125
    • 82=0.0156258^{-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,F0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
  • The letters AA through FF represent decimal values 1010 through 1515.
  • 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=25616^2 = 256
    • 161=1616^1 = 16
    • 160=116^0 = 1
    • ..
    • 161=0.062516^{-1} = 0.0625
    • 162=0.003916^{-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 00 to 1616:
DecimalBinaryOctalHexadecimal
00000000
11111111
2210102222
3311113333
441001004444
551011015555
661101106666
771111117777
8810001000101088
9910011001111199
1010101010101212AA
1111101110111313BB
1212110011001414CC
1313110111011515DD
1414111011101616EE
1515111111111717FF
1616100001000020201010

Binary Arithmetic

Binary Addition

  • There are four basic rules for adding binary digits:
    • 0+0=00 + 0 = 0
    • 0+1=10 + 1 = 1
    • 1+0=11 + 0 = 1
    • 1+1=101 + 1 = 10 (which is 00 carry 11)
  • Addition Examples:
    • Example 1 (Carries: 1,1,01, 1, 0): 10102+10012=1001121010_2 + 1001_2 = 10011_2
    • Example 2 (Carries: 0,1,10, 1, 1): 1012+0112=10002101_2 + 011_2 = 1000_2
    • Example 3 (Carries: 1,1,11, 1, 1): 11102+10112=1100121110_2 + 1011_2 = 11001_2
    • Example 4 (Carries: 1,1,1,11, 1, 1, 1): 101012+110112=110000210101_2 + 11011_2 = 110000_2

Binary Subtraction

  • Key concept: 102=(1×21)+(0×20)=21010_2 = (1 \times 2^1) + (0 \times 2^0) = 2_{10}.
  • When subtracting a larger digit from a smaller digit (e.g., 010 - 1), you must borrow from the next higher position. The borrow carries a value of 22 into the current position.
  • Subtraction Examples:
    • Example 1: 11021012110_2 - 101_2
      • Step: Borrow from the second position, making the first position 10210_2. 21=12 - 1 = 1. Result: 0012001_2.
      • Verification: 1102=610110_2 = 6_{10}, 1012=510101_2 = 5_{10}. 65=16 - 5 = 1.
    • Example 2: 1100021112=10001211000_2 - 111_2 = 10001_2
      • Verification: 110002=(1×24)+(1×23)=16+8=241011000_2 = (1 \times 2^4) + (1 \times 2^3) = 16 + 8 = 24_{10}. 1112=(1×22)+(1×21)+(1×20)=710111_2 = (1 \times 2^2) + (1 \times 2^1) + (1 \times 2^0) = 7_{10}. 247=171024 - 7 = 17_{10}, which is 10001210001_2.

Octal Arithmetic

Octal Addition

  • When the sum of two octal digits equals or exceeds 88, a carry is generated for the next higher position.
  • Example 1: 3568+1248356_8 + 124_8
    • 6+4=10106 + 4 = 10_{10}. Since 10810 ≥ 8, result is 108=210 - 8 = 2 with a carry of 11.
    • 5+2+1(carry)=8105 + 2 + 1 (carry) = 8_{10}. Since 888 ≥ 8, result is 88=08 - 8 = 0 with a carry of 11.
    • 3+1+1(carry)=53 + 1 + 1 (carry) = 5.
    • Therefore, 3568+1248=5028356_8 + 124_8 = 502_8.
  • Example 2: 10128+11481012_8 + 114_8 could be calculated similarly using base-8 rules.

Octal Subtraction

  • If borrowing is required, the borrow from the specific position adds 88 to the current digit.
  • Example 1: 25381478=1048253_8 - 147_8 = 104_8
    • Units: 3<73 < 7, so borrow from the tens position. This becomes (8+3)7=4(8 + 3) - 7 = 4.
    • Tens: Remaining 44=04 - 4 = 0.
    • Hundreds: 21=12 - 1 = 1.
  • Example 2: 1523836481523_8 - 364_8
    • Units: 3<43 < 4, borrow from tens: (8+3)4=7(8 + 3) - 4 = 7.
    • Tens: Remaining 1<61 < 6, borrow from hundreds: (8+1)6=3(8 + 1) - 6 = 3.
    • Hundreds: Remaining 43=14 - 3 = 1.
    • Thousands: 10=11 - 0 = 1.
    • Therefore, 152383648=113781523_8 - 364_8 = 1137_8.

Hexadecimal Arithmetic

Hexadecimal Addition

  • Addition follows standard rules, where values exceeding 1515 result in a carry-out to the next place value.
  • Example 1: 2316+1616=391623_{16} + 16_{16} = 39_{16}.
  • Example 2: 2B16+84162B_{16} + 84_{16}
    • B(11)+4=15=FB (11) + 4 = 15 = F.
    • 2+8=10=A2 + 8 = 10 = A.
    • Result: AF16AF_{16}.
  • Example 3: DF16+AC16DF_{16} + AC_{16}
    • F(15)+C(12)=2710F (15) + C (12) = 27_{10}. 2716=11(B)27 - 16 = 11 (B) with carry 11.
    • D(13)+A(10)+1(carry)=2410D (13) + A (10) + 1 (carry) = 24_{10}. 2416=824 - 16 = 8 with carry 11.
    • Result: 18B1618B_{16}.

Hexadecimal Subtraction

  • Example 1: 75162116=541675_{16} - 21_{16} = 54_{16}.
  • Example 2: 84162A1684_{16} - 2A_{16}
    • 4<A(10)4 < A (10), borrow 1616: (16+4)10=10=A(16 + 4) - 10 = 10 = A.
    • Remaining 72=57 - 2 = 5.
    • Result: 5A165A_{16}.

Number Base Conversions

Binary to Decimal

  • Multiply each binary digit by its weighted position value and sum the results.
  • Example 1: Convert 1101012110101_2 to decimal.
    • (1×25)+(1×24)+(0×23)+(1×22)+(0×21)+(1×20)=32+16+0+4+0+1=5310(1 \times 2^5) + (1 \times 2^4) + (0 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0) = 32 + 16 + 0 + 4 + 0 + 1 = 53_{10}.
  • Example 2: Convert 0.101120.1011_2 to decimal.
    • (1×21)+(0×22)+(1×23)+(1×24)=0.5+0+0.125+0.0625=0.687510(1 \times 2^{-1}) + (0 \times 2^{-2}) + (1 \times 2^{-3}) + (1 \times 2^{-4}) = 0.5 + 0 + 0.125 + 0.0625 = 0.6875_{10}.

Decimal to Binary

  • For integers: Repeatedly divide by 22 and collect the remainders from bottom to top.
  • Example 1: Convert 181018_{10} to binary.
    • 18/2=918 / 2 = 9 Remainder 00
    • 9/2=49 / 2 = 4 Remainder 11
    • 4/2=24 / 2 = 2 Remainder 00
    • 2/2=12 / 2 = 1 Remainder 00
    • 1/2=01 / 2 = 0 Remainder 11
    • Result: 10010210010_2.
  • For fractions: Repeatedly multiply by 22. If the result exceeds or equals 11, the carry is 11. Continue with the fractional part until it becomes zero.
  • Example 2: Convert 0.3125100.3125_{10} to binary.
    • 0.3125×2=0.6250.3125 \times 2 = 0.625 (Carry 00)
    • 0.625×2=1.2500.625 \times 2 = 1.250 (Carry 11)
    • 0.25×2=0.5000.25 \times 2 = 0.500 (Carry 00)
    • 0.50×2=1.0000.50 \times 2 = 1.000 (Carry 11)
    • Result: 0.010120.0101_2.

Octal to Decimal

  • Multiply each digit by its weighted position value (8n8^n) and sum.
  • Example 1: Convert 237.048237.04_8 to decimal.
    • (2×82)+(3×81)+(7×80)+(0×81)+(4×82)=128+24+7+0+0.0625=159.062510(2 \times 8^2) + (3 \times 8^1) + (7 \times 8^0) + (0 \times 8^{-1}) + (4 \times 8^{-2}) = 128 + 24 + 7 + 0 + 0.0625 = 159.0625_{10}.

Decimal to Octal

  • For integers: Repeatedly divide by 88 and collect remainders.
  • Example 1: Convert 35910359_{10} to octal.
    • 359/8=44359 / 8 = 44 Remainder 77
    • 44/8=544 / 8 = 5 Remainder 44
    • 5/8=05 / 8 = 0 Remainder 55
    • Result: 5478547_8.
  • For fractions: Repeatedly multiply by 88 and collect the carry integers.
  • Example 2: Convert 0.3125100.3125_{10} to octal.
    • 0.3125×8=2.500.3125 \times 8 = 2.50 (Carry 22)
    • 0.5×8=4.000.5 \times 8 = 4.00 (Carry 44)
    • Result: 0.2480.24_8.

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:1110: 000, 1: 001, 2: 010, 3: 011, 4: 100, 5: 101, 6: 110, 7: 111.
    • Example 1: Convert 13813_8 to binary. 10011 \rightarrow 001, 30113 \rightarrow 011. Total: 0010112001011_2.
    • Example 2: Convert 37.12837.12_8 to binary. 30113 \rightarrow 011, 71117 \rightarrow 111, 10011 \rightarrow 001, 20102 \rightarrow 010. Total: 011111.0010102011111.001010_2.

Hexadecimal to Decimal

  • Multiply each hexadecimal digit by its weighted position (16n16^n).
  • Example 1: Convert 35616356_{16} to decimal.
    • (3×162)+(5×161)+(6×160)=768+80+6=85410(3 \times 16^2) + (5 \times 16^1) + (6 \times 16^0) = 768 + 80 + 6 = 854_{10}.
  • Example 2: Convert AF216AF2_{16} to decimal.
    • (10×162)+(15×161)+(2×160)=2560+240+2=280210(10 \times 16^2) + (15 \times 16^1) + (2 \times 16^0) = 2560 + 240 + 2 = 2802_{10}.

Decimal to Hexadecimal

  • Repeatedly divide by 1616 and record remainders (convert remainders 101510–15 to letters AFA–F).
  • Example 1: Convert 65010650_{10}.
    • 650/16=40650 / 16 = 40 Remainder 10(A)10 (A)
    • 40/16=240 / 16 = 2 Remainder 88
    • 2/16=02 / 16 = 0 Remainder 22
    • Result: 28A1628A_{16}.

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:11110: 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 381638_{16} to binary. 300113 \rightarrow 0011, 810008 \rightarrow 1000. Total: 00111000200111000_2.
    • Example 2: Convert FB1716FB17_{16} to binary. F(1111),B(1011),1(0001),7(0111)F(1111), B(1011), 1(0001), 7(0111). Total: 111110110001011121111101100010111_2.
  • 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.