in this lecture: lecture 2: data...
TRANSCRIPT
E1.2 Digital Electronics 1 2.1 16 October 2008
Lecture 2: Data Representation
Dr Pete Sedcole
Department of E&E Engineering
Imperial College London
http://cas.ee.ic.ac.uk/~nps/
(Floyd 2.1 – 2.4, 2.8, 2.10 – 2.11)
(Tocci 2.1 – 2.8)
E1.2 Digital Electronics 1 2.2 16 October 2008
In this lecture:
• What do we mean by data?
• How can data be represented electronically?
• What number systems are usually used, and why?
• How do number systems in different bases work?
• How do you convert a number from decimal to binary and binary to decimal?
E1.2 Digital Electronics 1 2.3 16 October 2008
What do we mean by data?
• It depends on context, but we will say:
data = physical representation of information
• Data can be stored
– e.g.: computer disk, DVD, SIM card
• Data can be transmitted
– e.g.: fax, text message
• Data can be processed
– e.g.: cash till
E1.2 Digital Electronics 1 2.4 16 October 2008
Electronic representation of data
• Information can be complex
– e.g.: numbers, music, pictures
• What can we do using electronics?
– Set up voltages and currents
– Change voltages and currents
• A useful device is a switch
– closed: V = 0 Volts
– open: V = 5 Volts
5 Volts
Switch
R
V
E1.2 Digital Electronics 1 2.5 16 October 2008
• We can represent information using voltage levels
• The simplest information is TRUE/FALSE
– This can be represented by two voltage levels:
• 5 Volts = TRUE
• 0 Volts = FALSE
• A unit of information that has only two possible values is also called a BIT (short for Binary Digit)
• Why do we use binary data in electronics?
– Simple to implement in hardware: we only need
switches
– Good tolerance to noise
E1.2 Digital Electronics 1 2.6 16 October 2008
Number systems
Decimal
(base 10)
Octal
(base 8)
Binary
(base 2)
Hexadecimal
(base 16)
E1.2 Digital Electronics 1 2.7 16 October 2008
Decimal numbers
• Deci = ten
• The decimal number system has ten digits:0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
• The decimal number system has a base of 10, with each digit position weighted by a power of 10:
decimal point
10-410-310-210-1∏100101102103
ones
thou
sand
ths
tent
hs
hund
redt
hs
ten-
thou
sand
ths
thou
sand
s
tens
hund
reds
LSDMSD
MSD = most
significant digit
LSD = least
significant digit
E1.2 Digital Electronics 1 2.8 16 October 2008
Binary numbers
• Bi = two
• The binary number system has two digits:
0 and 1.
• The binary number system has a base of 2 with each
digit position weighted by a power of 2:
2-42-32-22-1∏20212223
binary point
MSB LSB
MSB = most
significant bit
LSB = least
significant bit
E1.2 Digital Electronics 1 2.9 16 October 2008
Binary number system
• A binary number uses the digits 0 and 1
• Subscript 2 indicates the number is in binary notation
(base 2)
• Example: 100112 is:
11001
2021222324
1 x 24 + 0 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 19
E1.2 Digital Electronics 1 2.10 16 October 2008
Integers and fractions in binary
• Binary numbers can represent fractional values as well
as integers
– e.g.: 10011.0112
• 10011.0112 is an 8-bit number
• It is in Q3 format – it has 3 bits after the binary point
• What if we want to represent (19.376)10 with an 8-bit
binary number?
0
2-1
1
2-2
1
20
∏ 11001
2-321222324
= (19.375)10
E1.2 Digital Electronics 1 2.11 16 October 2008
Conversion: decimal to binary (method 1)
• Express the decimal number as the sum of powers of 2
• 1s and 0s written in the corresponding bit positions
Example 1:
5010 = 32 + 18
= 32 + 16 + 2
= 1x25 + 1x24 + 1x21
5010 = 1100102
Example 2:
338.510 = 256 + 82.5
= 256 + 64 + 18.5
= 256 + 64 + 16 + 2.5
= 256 + 64 + 16 + 2 + 0.5
= 1x28 + 1x26 + 1x24 + 1x21 + 1x2-1
338.510 = 101010010.12
E1.2 Digital Electronics 1 2.12 16 October 2008
Conversion: decimal to binary (method 2)
• Repeated division
quotient remainder
50/2 = 25 0 LSB
25/2 = 12 1
12/2 = 6 0
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1 MSB
5010 = 1100102
E1.2 Digital Electronics 1 2.13 16 October 2008
Conversion: binary to decimal
• Determine the power of 2 for the position of each 1, and then sum
• Example:
(10101)2 = 24 + 22 + 20 = 16 + 4 + 1 = (21)10
• Not the only way to do it…
E1.2 Digital Electronics 1 2.14 16 October 2008
Binary addition
• Firstly, recall decimal addition:
6302Sum
287+B
4521A
11
• Binary addition follows the same pattern, but
0 + 0 = 0 carry 0
0 + 1 = 1 + 0 = 1 carry 0
1 + 1 = 0 carry 1
1 + 1 + 1 = 1 carry 11011Sum
0110+B
1110A
11
E1.2 Digital Electronics 1 2.15 16 October 2008
Binary addition
• Note that to calculate each bit sn of the sum, we need to
consider the values of 3 input bits:
– The corresponding bits of A and B, (an and bn)
– The carry-out from the previous addition
• Each bit or column of the binary addition generates two outputs
– sum sn and carry-out
E1.2 Digital Electronics 1 2.16 16 October 2008
Hexadecimal numbers
• Hexadeci = sixteen
• Base 16 representation
• Easy to convert to and from binary numbers
• More compact to write, easier for us to read than binary
E1.2 Digital Electronics 1 2.17 16 October 2008
Hexadecimal number conversions
• Binary-to-hexadecimal
1. Separate the binary number into 4-bit groups
2. Replace each group with the hexadecimal equivalent
• Hexadecimal-to-decimal
1. Re-write each hexadecimal digit as the 4-bit binary equivalent
2. Convert the binary number to decimal
• Decimal-to-hexadecimal
– Repeated division by 16
E1.2 Digital Electronics 1 2.18 16 October 2008
Binary Coded Decimal (BCD)
• Use 4-bit binary to represent one decimal digit
• Easy to convert between decimal ↔ binary
• Wastes bits (4 bits can represent 16 values, but only 10 values are used)
• Used extensively in financial calculations
E1.2 Digital Electronics 1 2.19 16 October 2008
Binary Coded Decimal (BCD)
• Convert 0110100100010111 (BCD) to its decimal equivalent:
0110 1001 0001 0111
6 9 1 7
• Convert the BCD number 011111000001 to decimal
0111 1100 0001
7 ? 1
The forbidden code group indicates an error has occurred
E1.2 Digital Electronics 1 2.20 16 October 2008
Putting it all together
E1.2 Digital Electronics 1 2.21 16 October 2008
Gray Codes
• Only one bit in the code
changes in the sequence
• Useful for industrial control
E1.2 Digital Electronics 1 2.22 16 October 2008
• Binary code results in glitches
• Gray code avoids glitches
E1.2 Digital Electronics 1 2.23 16 October 2008
ASCII code
• Codes representing letters of the alphabet, punctuation marks and other special characters are called alphanumeric codes
• The most widely used alphanumeric code is the American Standard Code for Information Interchange, or ASCII
• ASCII is pronounced “askee”
• It is a 7-bit code