CS 61C SU20
Lec 1 Number Representation
Number Bases
General Formula:
n-digit number in base B $$ d_{n-1}d_{n-2}...d_{1}d_{0}=d_{n-1}B^{n-1}+d_{n-2}B^{n-2}+...d_{1}B^{1}+d_{0}B^{0} $$ a digit d can take on value from 0 to B - 1
n digits (base B) => <= B^n things
n bits => 2^n things
Decimal | Binary | Hexadecimal |
---|---|---|
0 | 0b0000 | 0x0 |
1 | 0b0001 | 0x1 |
2 | 0b0010 | 0x2 |
3 | 0b0011 | 0x3 |
4 | 0b0100 | 0x4 |
5 | 0b0101 | 0x5 |
6 | 0b0110 | 0x6 |
7 | 0b0111 | 0x7 |
8 | 0b1000 | 0x8 |
9 | 0b1001 | 0x9 |
10 | 0b1010 | 0xA |
11 | 0b1011 | 0xB |
12 | 0b1100 | 0xC |
13 | 0b1101 | 0xD |
14 | 0b1110 | 0xE |
15 | 0b1111 | 0xF |
Unsigned Integers
Represent only non-negative (Uunsigned) integers.
Signed Representations
"first" bit gives sign, rest treated as unsigned (magnitude)
The disadvantage of this representation is that can not represent all positive number. For example, in a 4-bit binary signed representation, the largest positive number that can be represented is 0111 (7), and the smallest negative number that can be represented is 1000 (-8), thus the number 8 cannot be represented.
Binary | Decimal |
---|---|
000 | 0 |
001 | 1 |
010 | 2 |
011 | 3 |
100 | 0 |
101 | -1 |
110 | -2 |
111 | -3 |
Biased Notation
In biased notation, the binary representation of a number is its two's complement relative to the bias. For example, in an 8-bit biased notation system with a bias of 127, the two's complement of the number 0 would be 01111111, because it is to the left of the bias, while the two's complement of the number 255 would be 11111111, because it is to the right of the bias.
We usually use the (2^(n-1) - )1 as the bias to represent the zero.
The other way to represent the bias is using the absolute value of the the most minimum negative value.
One's Complement(反码)
In computer science, one's complement is a simple binary representation of a number that is obtained by flipping all the bits of the number. More specifically, one's complement is obtained by subtracting each bit from 1.
For example, the one's complement of the binary number 101 is 010. This means that each 1 in the original binary number is replaced with a 0, and each 0 is replaced with a 1.
Two's Complement
Two's complement is a mathematical operation that is used to represent signed numbers in binary form. In two's complement, the most significant bit (leftmost bit) is used as a sign bit, where 0 represents a positive number and 1 represents a negative number.
To obtain the two's complement of a binary number, we first take the one's complement of the number (complementing all the bits), and then add 1 to the result.
For example two's complement of -3 in 3-bit representation. First, we need to convert 3 to binary, which is 0011. Then, we need to flip it to get 1100, and add 1 to it to get 1101, which is the binary two's complement of -3.
Overflow
Sign Extension
Conversions
Hex <=> Binary <=> Decimal
Binary | Decimal |
---|---|
$2^0$ | 1 |
$2^1$ | 2 |
$2^2$ | 4 |
$2^3$ | 8 |
$2^4$ | 16 |
$2^5$ | 32 |
$2^6$ | 64 |
$2^7$ | 128 |
$2^8$ | 256 |
$2^9$ | 512 |
$2^{10}$ | 1024 |
Binary | Unit |
---|---|
$2^{10}$ | Kibi |
$2^{20}$ | Mebi |
$2^{30}$ | GiBi |
$2^{40}$ | TeBi |
$2^{50}$ | PeBi |
$2^{60}$ | Exbi |
$2^{70}$ | Zebi |
$2^{80}$ | Yobi |