Computer Architecture

Last Modified: 1/17/2025

Computer Architecture

In this part, we will explore computer architecture, both x86-64 and RISC-V architecture.

1 Number Representation

1.1 Number Base

In computer science, There are three commonly-used number bases: binary, decimal & hexadecimal.

  1. Binary (base 2)
    • Symbols: 0, 1
    • Notation: 1010112=0b101011101011_2=\texttt{0b101011}
    • Converting numbers to base 2 lets us represent numbers as bits!
  2. Decimal (base 10)
    • Symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    • Notation: 947210=94729472_{10}=9472
    • Understandable by humans, used in our daily life.
  3. Hexadecimal (base 16)
    • Symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
    • Notation: 2A5D16=0x224002A5D_{16}=\texttt{0x22400}
    • A convenient shorthand for writing long sequences of bits.
Conversion between bases
  1. Convert from other bases to base-10: write out power of bases.

    For example, AC216=(10×162)+(12×161)+(2×160)=2754AC2_{16}=(10 \times 16^2)+(12 \times 16^1)+(2 \times 16^0)=2754

  2. Convert from base-10 to other bases: Use the “leftover algorthm”

    For example, convert 731073_{10} to base-4. For base-4, the powers of base include 256, 64, 16, 4, 1.

    • How many multiples of 6464 fit in 7373? 7364=973 - 64 = 9 left over.
    • How many multiples of 1616 fit in 99? Still 99 left over.
    • How many multiples of 44 fit in 99? 92×4=19 - 2 \times 4 = 1 left over.
    • How many multiples of 11 fit in 11? 11=01 - 1 = 0, which means we are done!

    Therefore, 7310=1021473_{10}=1021_4.

When converting between different bases, you can refer to the following table below.

DecimalBinaryHexadecimal
000000000000
110001000111
220010001022
330011001133
440100010044
550101010155
660110011066
770111011177
881000100088
991001100199
101010101010AA
111110111011BB
121211001100CC
131311011101DD
141411101110EE
151511111111FF

Aside

  1. Beware of padding with zeros! When converting from binary to hexadecimal, left-padding if needed (0b110010=0b0011 0010=0x32\texttt{0b110010} = \texttt{0b0011 0010} = \texttt{0x32}); when converting from hexadecimal, drop leading zeros if needed (0x1D=0b0001 1101=0b11101\texttt{0x1D} = \texttt{0b0001 1101} = \texttt{0b11101}).
  2. 1 bytes = 8 bits, 1 nibble = 4 bits.

1.2 Integer Representation

1.2.1 Unsigned Integers

Properties

Conclusion

Unsigned Integer
Can represent negative numbers
Doing math is easy
Every bit sequence represents a unique number

1.2.2 Signed Integers

Properties

Note

If we count upwards in base-2, the resulting numbers increase, then they start decreasing!

Plus, there are two ways of representing zero: 0b100...00\texttt{0b100...00} & 0b000..00\texttt{0b000..00}.

Signed Integer
Sign-Magnitude
Can represent negative numbers
Doing math is easy
Every bit sequence represents a unique number

1.2.3 One’s Complement

Properties

Note

If we count upwards in base-2, the resulting numbers are always increasing.

There are two ways of representing zero: 0b111...11\texttt{0b111...11} & 0b000..00\texttt{0b000..00}.

One's Complement
One’s Complement
Can represent negative numbers
Doing math is easy
Every bit sequence represents a unique number

1.2.4 Two’s Complement

Properties

Aside

Another definition: The left-most power of 2 is now negative, not positive.

  1. Left-most bit 0: Read the rest of the number as an unsigned integer.
  2. Left-most bit 1: Subtract a big power of 2. Resulting number is negative!
  3. For example, 00000000-01110111 represent 00-77, while 10001000-11111111 represent 8-8-1-1.

Conversion between two’s complement and signed integer

  1. Two’s Complement -> Signed Integer

    • If left-most digit is 0: Read it as unsigned
    • If left-most digit is 1:
      • Flip the bits, and add 1
      • Convert to base-10, and stick a negative sign in front

    Example: What is 0b1110 1100\texttt{0b1110 1100} in decimal?

    • Flip the bits: 0b0001 0011\texttt{0b0001 0011}
    • Add one: 0b0001 0100\texttt{0b0001 0100}
    • In base-10: 20-20
  2. Signed Integer -> Two’s Complement

    • If number is positive: Just convert it to base-2
    • If number is negative:
      • Pretend it’s unsigned, and convert to base-2
      • Flip the bits, and add 1

    Example: What is 20-20 in two’s complement binary?

    • In base-2: 0b0001 0100\texttt{0b0001 0100}
    • Flip the bits: 0b1110 1011\texttt{0b1110 1011}
    • Add one: 0b1110 1100\texttt{0b1110 1100}
Two’s Complement
Can represent negative numbers
Doing math is easy
Every bit sequence represents a unique number