Data Representation

advertisement
Data Representation
Bits, Bytes, Binary, Hexadecimal
Computer Science “Joke”
• There are 10 types of people in the world:
Those who understand binary…
And those who don’t.
• Get it?
• If not, you will by the end of today’s lecture..
1
Outline
• Bits and Bytes
• Binary
• Hexadecimal
2
Bits and Bytes
• Recall input / output of Logic Gates
• 0 => FALSE
• 1 => TRUE
A B
A
B
3
C
C =A&B
0
0
0
0
1
0
1
0
0
1
1
1
Bits and Bytes
• In reality, 0 and 1 represent voltage states (e.g., LOW / HIGH)
• These voltage states give the electrical circuit “life”
A B
A
B
4
C
C =A&B
0
0
0
0
1
0
1
0
0
1
1
1
Bits and Bytes
• Think of each input / output as a “light switch”
• Can be either “on” (TRUE, 1) or “off” (FALSE, 0)
5
Bits and Bytes
•
•
•
•
Each “light switch” represents a bit
bit stands for Binary digIT
A bit can be either 0 or 1
A bit is the most basic element in computing
• Basis for logic gate circuits
• (logic gate circuits => more complex circuits)
6
Bits and Bytes
• When you put 8 bits together, you get a byte
• A byte is used to encode integers from 0 to 255
• Or integers from -128 to 127
• (more on this later)
7
Bits and Bytes
• In computers, a byte is the smallest addressable memory unit
• Memory units are defined in terms of bytes (not bits)
• E.g., Kilobytes (KB), Megabytes (MB), Gigabytes (GB), etc.
• Kilobyte: 1,000 bytes (thousand)
• Megabyte: 1,000,000 bytes (million)
• Gigabyte: 1,000,000,000 bytes (billion)
8
Bits and Bytes
• How many bits in 10 GB of storage?
9
Bits and Bytes
• How many bits in 10 GB of storage?
80,000,000,000 bits
bytes
bits
10 GB ´ 1,000,000,000
´8
= 80,000,000,000 bits
GB
byte
10
Outline
• Bits and Bytes
• Binary
• Hexadecimal
11
Binary Numbers
• A computer’s machine language only uses ONES and ZEROS
• How does a computer represent a number using only bits?
12
Binary Numbers
• A byte can represent any number between 0 and 255
• A byte is a sequence of eight bits, b7 through b0
b7 b6 b5 b4 b3 b2 b1 b0
13
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
b7 × (27)
b6 × (26)
b5 × (25)
b4 × (24)
b3 × (23)
b2 × (22)
b1 × (21)
b0 × (20)
14
NUMBER
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
b7 through b0
can only be
0 or 1
15
b7 × (27)
b6 × (26)
b5 × (25)
b4 × (24)
b3 × (23)
b2 × (22)
b1 × (21)
b0 × (20)
NUMBER
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
b7 × (27)
b6 × (26)
b5 × (25)
b4 × (24)
b3 × (23)
b2 × (22)
b1 × (21)
b0 × (20)
16
NUMBER
Note how b7 gets
multiplied by 27
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
b7 × (27)
b6 × (26)
b5 × (25)
b4 × (24)
b3 × (23)
b2 × (22)
b1 × (21)
b0 × (20)
17
NUMBER
… b6 gets
multiplied by 26
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
b7 × (27)
b6 × (26)
b5 × (25)
b4 × (24)
b3 × (23)
b2 × (22)
b1 × (21)
b0 × (20)
18
NUMBER
… b5 gets
multiplied by 25
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
b7 × (27)
b6 × (26)
b5 × (25)
b4 × (24)
b3 × (23)
b2 × (22)
b1 × (21)
b0 × (20)
19
NUMBER
Et.
cetera
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
All the
multiplications get
added together into
the integer number
20
b7 × (27)
b6 × (26)
b5 × (25)
b4 × (24)
b3 × (23)
b2 × (22)
b1 × (21)
b0 × (20)
NUMBER
Binary Numbers
b7 b6 b5 b4 b3 b2 b1 b0
b7 × (128)
b6 × (64)
b5 × (32)
b4 × (16)
b3 × (8)
b2 × (4)
b1 × (2)
b0 × (1)
21
NUMBER
Binary Numbers: Example
0010 1010
Represents what number?
22
Binary Numbers: Example
0010 1010
0 × (128)
0 × (64)
1 × (32)
0 × (16)
1 × (8)
0 × (4)
1 × (2)
0 × (1)
23
Binary Numbers: Example
0010 1010
0 × (128)
0 × (64)
1 × (32)
0 × (16)
1 × (8)
0 × (4)
1 × (2)
0 × (1)
24
32 + 8 + 2 = 42
Binary Numbers: Example
0010 1010
Represents what number?
42
25
Binary Numbers: Example
1001 0111
Represents what number?
• Please find a partner to work with
• Check each other’s work
26
Binary Numbers: Example
1001 0111
Represents what number?
151
27
Binary Numbers
• Same technique applies to represent larger numbers
• 2 bytes (16 bits)
• 4 bytes (32 bits)
• 8 bytes (64 bits)
• What’s the largest unsigned (positive only) number that can be
represented using 16 bits?
• Hint: the largest number for 8-bits is 255…
• We’ll cover signed (positive and negative) numbers next time…
28
Binary Numbers
• Example of 16-bit number:
1000 0000 0000 0001
29
Binary Numbers
• Example of 16-bit number:
1000 0000 0000 0001
= 215 + 20
= 32,768 + 1
= 32,769
30
Binary Numbers
• To convert decimal to binary…
• “Reverse” the process…
1.
2.
3.
4.
31
Find largest power of 2 that “fits” within the decimal value
Record 1 in the associated log2 placeholder (in binary sequence)
Subtract power of 2 value from original decimal number
Repeat until nothing remains
Binary Numbers
• Example: Decimal to Binary
• What is the binary sequence for decimal number 250?
1. Find largest power of two that fits in 250
128
2. Record 1 in associated placeholder
(128 = 27 , 7th position) => 1000 0000
3. Subtract power of two value from original decimal
(250 – 128 = 122)
4. Repeat until there’s nothing left.
32
Binary Numbers
• Example: Decimal to Binary
1. Find largest power of two that fits in 122
64
2. Record 1 in associated placeholder
(64 = 26 , 6th position) => 1100 0000
3. Subtract power of two value from original decimal
( 122 – 64 = 58 )
4. Repeat until there’s nothing left.
33
Binary Numbers
• Example: Decimal to Binary
1. Find largest power of two that fits in 58
32
2. Record 1 in associated placeholder
(32 = 25 , 5th position) => 1110 0000
3. Subtract power of two value from original decimal
( 58 - 32 = 26)
4. Repeat until there’s nothing left.
34
Binary Numbers
• Example: Decimal to Binary
1. Find largest power of two that fits in 26
16
2. Record 1 in associated placeholder
(16 = 24 , 4th position) => 1111 0000
3. Subtract power of two value from original decimal
( 26 – 16 = 10)
4. Repeat until there’s nothing left.
35
Binary Numbers
• Example: Decimal to Binary
1. Find largest power of two that fits in 10
8
2. Record 1 in associated placeholder
(8 = 23 , 3th position) => 1111 1000
3. Subtract power of two value from original decimal
( 10– 8 = 2 )
4. Repeat until there’s nothing left.
36
Binary Numbers
• Example: Decimal to Binary
1. Find largest power of two that fits in 2
2
2. Record 1 in associated placeholder
(2 = 21 , 1st position) => 1111 1010
3. Subtract power of two value from original decimal
(2–2=0)
4. Repeat until there’s nothing left.
DONE!!
37
Binary Numbers
• What is the binary sequence for decimal number 250?
250 = 128 + 64 + 32 + 16 + 8 + 2
= 27 + 26 + 25 + 24 + 23 + 21
= 1111 1010
38
Break Time…
39
Outline
• Bits and Bytes
• Binary
• Hexadecimal
40
Hexadecimal
• Writing and representing binary numbers can be cumbersome
• Hard to read / write large sequences of 0s and 1s
• Hexadecimal offers succinct way to represent binary numbers
• E.g.,
1011 0101 = 0xB5
41
Hexadecimal
• Decimal numbers are base 10
• 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Hexadecimal numbers are base 16
• 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
• Note: we prefix hexadecimal numbers with 0x
• Why use hex?
• Easier to represent binary numbers…
42
Hexadecimal
• 4-bit numbers go from 0 to 15
• 0000 => 0
• 1111 => 15
• Hexadecimal is base 16
• Can easily represent numbers between 0 and 15
• 0 => 0x0
• 15 => 0xF
• Thus, we can represent
• 0000 as 0x0
• 1111 as 0xF
43
Hexadecimal
• This can be abstracted to 8-bit numbers…
• One hex number represents the first 4-bits
• One hex number represents the second 4-bits
1011 0101
1011 = 11 = 0xB
44
0101 = 5 = 0x5
0xB5
Hexadecimal
• This works with 16, 32, and 64-bit numbers
• One hex number per 4-bits
1111 0110 1100 0001
0xF
45
0x6
0xC
0xF6C1
0x1
Hexadecimal
1111 0110 1100 0001 (binary)
= 0xF6C1 (hex)
= 63,169 (decimal)
46
Hexadecimal (practice)
• Find a partner to work with
• Convert decimal number 77
A) to binary
B) to hexadecimal
47
Hexadecimal (practice)
• Find a partner to work with
• Convert decimal number 77
A) to binary
01001101
B) to hexadecimal
0x4D
48
Hexadecimal (practice)
• Convert binary number 1110 0111
A) to decimal
B) to hexadecimal
49
Hexadecimal (practice)
• Convert binary number 1110 0111
A) to decimal
231
B) to hexadecimal
0xE7
50
Hexadecimal (practice)
• Convert hexadecimal number 0xDF
A) to binary
B) to decimal
51
Hexadecimal (practice)
• Convert hexadecimal number 0xDF
A) to binary
11011111
B) to decimal
223
52
Next steps…
• Next lecture:
• Binary Arithmetic (addition / subtraction)
• 2’s complement (signed numbers)
• Simple logic gate circuits to do addition…
53
Download