CS 202 - Notes 2016-02-24

It’s all about the bits

All information in the computer are stored as as patterns of bits.

Given N bits, we can make 2N different patterns. These could be numbers, letters, or anything else we want to represent.

basic sizes:bit, byte (8 bits)

Prefix (base 10) Meaning (base 10) Abbrv (base 10) Prefix (base 2) Meaning (base 2) Abbrv (base 2)
kilo- 103 KB 210 kibi- KiB
mega- 106 MB 220 mebi- MiB
giga- 109 GB 230 gibi- GiB
tera- 1012 TB 240 tebi- TiB
peta- 1015 PB 250 pebi- PiB
exa- 1018 EB 260 exbi- EiB
zetta- 1021 ZB 270 zebi- ZiB
yotta- 1024 YB 280 yobi- YiB

All of the abbreviations are in bytes. If we want to talk about bits, we use a lowercase b (Kb, Mb, etc…)

The base two prefixes have not fully caught on. This is problematic because there is an increasing discrepancy between the base 10 and base 2 meanings.

1000 vs 1024 (K vs Ki) is not a huge difference 1000000000 vs 1073741824 (G vs. Gi) starts being a bit significant

Another unit of size is the word. In theory, this is the native unit of size for the computer, but for reasons of compatibility, on our machines, this has been frozen at 16 bits for some time.

Numbers and Bases

To make it easier to deal with bit patterns, we treat them as if they were binary numbers.

Decimal values are convenient, but it isn’t easy to do conversions because 10 is not a power of two. So, we frequently will make use of other bases that are more compact than binary, but still powers of two.

Octal (base 8)

In base 8, each digit can be one of eight values (0,1,2,3,4,5,6,7).

Conversion is simple, because we exhaust all combinations of three bits in eight combinations as well. So, we can convert from binary to octal by breaking the pattern into blocks of three and converting each block separately. Conversely, converting from octal to binary just means converting each digit of octal to the corresponding three bits of binary.

Decimal Binary Octal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 10
9 1001 11
10 1010 12

Example: 1 011 011 1012 = 13358

Example: 7268 = 111 010 1102

Hexadecimal (base 16)

Base 16 is more common because it is more compact. Each digit can be one of 16 values (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).

Decimal Binary Octal
0 00000 0
1 00001 1
2 00010 2
3 00011 3
4 00100 4
5 00101 5
6 00110 6
7 00111 7
8 01000 8
9 01001 9
10 01010 A
11 01011 B
12 01100 C
13 01101 D
14 01110 E
15 01111 F
16 10000 10
17 10001 11

Example: 10 1011 01102 = 2B616

Example: 5AF16 = 0101 1010 11112

Converting to decimal from other bases

We still want to know the value of the number (which means the decimal representation since we think in decimal). We can convert a number in any base b to decimal using this equation:

d3d2d1d0 = d3 * b3 + d2 * b2 + d1 * b1 + d0 * b0

Example: 110112 = 1 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 = 16 + 8 + 2 + 1 = 27

Example: 2A16 = 2 * 16 + 10 * 1 = 42