Computer Architecture
Last Modified: 1/17/2025
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.
- Binary (base 2)
- Symbols: 0, 1
- Notation:
- Converting numbers to base 2 lets us represent numbers as bits!
- Decimal (base 10)
- Symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- Notation:
- Understandable by humans, used in our daily life.
- Hexadecimal (base 16)
- Symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- Notation:
- A convenient shorthand for writing long sequences of bits.
-
Convert from other bases to base-10: write out power of bases.
For example,
-
Convert from base-10 to other bases: Use the “leftover algorthm”
For example, convert to base-4. For base-4, the powers of base include 256, 64, 16, 4, 1.
- How many multiples of fit in ? left over.
- How many multiples of fit in ? Still left over.
- How many multiples of fit in ? left over.
- How many multiples of fit in ? , which means we are done!
Therefore, .
When converting between different bases, you can refer to the following table below.
Decimal | Binary | Hexadecimal |
---|---|---|
Aside
- Beware of padding with zeros! When converting from binary to hexadecimal, left-padding if needed (); when converting from hexadecimal, drop leading zeros if needed ().
- 1 bytes = 8 bits, 1 nibble = 4 bits.
1.2 Integer Representation
1.2.1 Unsigned Integers
Properties
- Idea: Simply convert decimal to binary, cCan represent different numbers!
- Smallest number: represents .
- Largest number: represents .
Conclusion
Unsigned Integer | |
---|---|
Can represent negative numbers | ✘ |
Doing math is easy | ✔ |
Every bit sequence represents a unique number | ✔ |
1.2.2 Signed Integers
Properties
- Idea: Use the left-most bit to indicate if the number is positive (0) or negative (1). This is called sign-magnitude representation.
- Smallest number: represents .
- Largest number: represents .
Note
If we count upwards in base-2, the resulting numbers increase, then they start decreasing!
Plus, there are two ways of representing zero: & .
Sign-Magnitude | |
---|---|
Can represent negative numbers | ✔ |
Doing math is easy | ✘ |
Every bit sequence represents a unique number | ✘ |
1.2.3 One’s Complement
Properties
- Idea: If the number is negative, flip the bits. For example, is , so is . Left-most bit acts like a sign bit. If it’s 1, someone flipped the bits, so number must be negative.
- Smallest number: represents .
- Largest number: represents .
Note
If we count upwards in base-2, the resulting numbers are always increasing.
There are two ways of representing zero: & .
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
- Idea: If the number is negative, flip the bits, and add one (because we shift to avoid double-zero). Because of overflow, addition behaves like modular arithmetic (For example, and are the same in land: ).
- Smallest number: represents .
- Largest number: represents .
Aside
Another definition: The left-most power of 2 is now negative, not positive.
- Left-most bit 0: Read the rest of the number as an unsigned integer.
- Left-most bit 1: Subtract a big power of 2. Resulting number is negative!
- For example, - represent -, while - represent -.
Conversion between two’s complement and signed integer
-
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 in decimal?
- Flip the bits:
- Add one:
- In base-10:
-
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 in two’s complement binary?
- In base-2:
- Flip the bits:
- Add one:
Two’s Complement | |
---|---|
Can represent negative numbers | ✔ |
Doing math is easy | ✔ |
Every bit sequence represents a unique number | ✔ |