1
EE 109 Unit 3 Binary Representation Systems 2 ANALOG VS. DIGITAL - - PowerPoint PPT Presentation
EE 109 Unit 3 Binary Representation Systems 2 ANALOG VS. DIGITAL - - PowerPoint PPT Presentation
1 EE 109 Unit 3 Binary Representation Systems 2 ANALOG VS. DIGITAL 3 Analog vs. Digital The analog world is based on continuous events. Observations can take on (real) any value. The digital world is based on discrete events.
2
ANALOG VS. DIGITAL
3
Analog vs. Digital
- The analog world is based on continuous
- events. Observations can take on (real) any
value.
- The digital world is based on discrete events.
Observations can only take on a finite number
- f discrete values
4
Analog vs. Digital
- Q. Which is better?
- A. Depends on what you are trying to do.
- Some tasks are better handled with analog
data, others with digital data.
– Analog means continuous/real valued signals with an infinite number of possible values – Digital signals are discrete [i.e. 1 of n values]
5
Analog vs. Digital
- How much money is in my checking account?
– Analog: Oh, some, but not too much. – Digital: $243.67
6
Analog vs. Digital
- How much do you love me?
– Analog: I love you with all my heart!!!! – Digital: 3.2 x 103 MegaHearts
7
Digital is About Numbers
- In a digital world, numbers are used to represent all
the possible discrete events
– Numerical values – Computer instructions (ADD, SUB, BLE, …) – Characters ('a', 'b', 'c', …) – Conditions (on, off, ready, paper jam, …)
- Numbers allow for easy manipulation
– Add, multiply, compare, store, …
- Results are repeatable
– Each time we add the same two number we get the same result
8
The Real (Analog) World
- The real world is inherently analog.
- To interface with it, our digital systems need
to:
– Convert analog signals to digital values (numbers) at the input. – Convert digital values to analog signals at the
- utput.
- Analog signals can come in many forms
– Voltage, current, light, color, magnetic fields, pressure, temperature, acceleration, orientation
9
DIGITAL REPRESENTATION
10
Interpreting Binary Strings
- Given a string of 1’s and 0’s, you need to know the
representation system being used, before you can understand the value of those 1’s and 0’s.
- Information (value) = Bits + Context (System)
01000001 = ?
6510 ‘A’ASCII 41BCD
Unsigned Binary system ASCII system BCD System
11
Binary Representation Systems
- Integer Systems
– Unsigned
- Unsigned (Normal) binary
– Signed
- Signed Magnitude
- 2’s complement
- Excess-N*
- 1’s complement*
- Floating Point
– For very large and small (fractional) numbers
- Codes
– Text
- ASCII / Unicode
– Decimal Codes
- BCD (Binary Coded Decimal)
/ (8421 Code)
* = Not fully covered in this class
12
Number Systems
- Number systems consist of
- 1. A base (radix) r
- 2. r coefficients [0 to r-1]
- Human System: Decimal (Base 10):
0,1,2,3,4,5,6,7,8,9
- Computer System: Binary (Base 2): 0,1
- Human systems for working with computer systems
(shorthand for human to read/write binary)
– Octal (Base 8): 0,1,2,3,4,5,6,7 – Hexadecimal (Base 16): 0-9,A,B,C,D,E,F (A thru F = 10 thru 15)
13
Anatomy of a Decimal Number
- A number consists of a string of explicit coefficients (digits).
- Each coefficient has an implicit place value which is a power
- f the base.
- The value of a decimal number (a string of decimal
coefficients) is the sum of each coefficient times it place value
Explicit coefficients Implicit place values
radix (base)
(934)10 = 9*102 + 3*101 + 4*100 = 934 (3.52)10 = 3*100 + 5*10-1 + 2*10-2 = 3.52
14
Positional Number Systems (Unsigned)
- A number in base r has place values/weights that are
the powers of the base
- Denote the coefficients as: ai
r -1 r -2 r 1 r 0
.
r 2 r 3
Left-most digit = Most Significant Digit (MSD) Right-most digit = Least Significant Digit (LSD)
... ...
a -1 a -2 a 1 a 0 a 2 a 3
Nr = Σi(ai*ri) = D10
15
Examples
(746)8 = 7*82 + 4*81 + 6*80 = 448 + 32 + 16 = 48610 (1A5)16 = 1*162 + 10*161 + 5*160 = 256 + 160 + 5 = 42110
16
Anatomy of a Binary Number
- Same as decimal but now the coefficients are
1 and 0 and the place values are the powers of 2 (1011)2 = 1*23 + 0*22 + 1*21 + 1*20
Least Significant Bit (LSB) Most Significant Digit (MSB) coefficients place values = powers of 2 radix (base)
17
Binary Examples
(1001.1)2 = 8 + 1 + 0.5 = 9.510
.5 1 2 4 8
(10110001)2 = 128 + 32 + 16 + 1 = 17710
16 32 128 1
18
Powers of 2
20 = 1 21 = 2 22 = 4 23 = 8 24 = 16 25 = 32 26 = 64 27 = 128 28 = 256 29 = 512 210 = 1024
512 256 128 64 32 16 8 4 2 1 1024
19
Practice On Your Own
(11010)2 = 1*24 + 1*23 + 1*21 = 16 + 8 + 2 = (26)10 (6523)8 = 6*83 + 5*82 + 2*81 + 3*80 = 3072 + 320 + 16 + 3 = (3411)10 (AD2)16 = 10*162 + 13*161 + 2*160 = 2560 + 208 + 2 = (2770)10
- Decimal equivalent is…
… the sum of each coefficient multiplied by its implicit place value (power of the base) = Σi(ai * ri) [ai = coefficient, r = base]
20
Unique Combinations
- Given n digits of base r, how many unique numbers
can be formed? rn
– What is the range? [0 to rn-1]
Main Point: Given n digits of base r, rn unique numbers can be made with the range [0 - (rn-1)]
2-digit, decimal numbers (r=10, n=2) 3-digit, decimal numbers (r=10, n=3) 4-bit, binary numbers (r=2, n=4) 6-bit, binary numbers (r=2, n=6)
0-9 0-9 100 combinations: 00-99 0-1 0-1 0-1 0-1 1000 combinations: 000-999 16 combinations: 0000-1111 64 combinations: 000000-111111
21
Approximating Large Powers of 2
- Often need to find decimal
approximation of a large powers of 2 like 216, 232, etc.
- Use following approximations:
– 210 ≈ 103 (1 thousand) = 1 Kilo- – 220 ≈ 106 (1 million) = 1 Mega- – 230 ≈ 109 (1 billion) = 1 Giga- – 240 ≈ 1012 (1 trillion) = 1 Tera-
- For other powers of 2, decompose
into product of 210 or 220 or 230 and a power of 2 that is less than 210
– 16-bit half word: 64K numbers – 32-bit word: 4G numbers – 64-bit dword: 16 million trillion numbers
216 = 26 * 210 ≈ 64 * 103 = 64,000 224 = 24 * 220 ≈ 16 * 106 = 16,000,000 228 = 28 * 220 ≈ 256 * 106 = 256,000,000 232 = 22 * 230 ≈ 4 * 109 = 4,000,000,000
22
Decimal to Unsigned Binary
- To convert a decimal number, x, to binary:
– Only coefficients of 1 or 0. So simply find place values that add up to the desired values, starting with larger place values and proceeding to smaller values and place a 1 in those place values and 0 in all others
16 8 4 2 1
2510 = 1 1 1
32 For 2510 the place value 32 is too large to include so we include
- 16. Including 16 means we have to make 9 left over. Include 8
and 1.
23
Decimal to Unsigned Binary
7310=
128 64 32 16 8 4 2 1 .5 .25 .125 .0625 .03125
0 1 0 0 1 0 0 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 0 1 1 0 1 0 0 8710= 14510= 0.62510=
24
Decimal to Another Base
- To convert a decimal number, x, to base r:
– Use the place values of base r (powers of r). Starting with largest place values, fill in coefficients that sum up to desired decimal value without going over.
16 1
7510 = 4 B
256 hex
25
Timeout-Liars & Truth Tellers
- You're walking on an island with a volcano and natives who are either truth tellers or liars. You meet a
native, and you want to know which kind of person he is. So you ask him
– “Are you a truth-teller? ” – When he is answering, the volcano makes a loud noise and you cannot hear the answer. So you ask him again – “Excuse me, I couldn't hear what you said, did you say you were a truth-teller?” - and he answers – “No, I didn't say that, I said I was a liar. “
- Is the native a liar or a truth-teller? (Hint: Think about what the native's could have answered to the
first question, first assuming he's a truth-teller and then assuming he's a liar.)
http://math.berkeley.edu/~antonio/MEC/liars.html
26
SIGNED SYSTEMS
Signed Magnitude 2’s Complement System
27
Binary Representation Systems
- Integer Systems
– Unsigned
- Unsigned (Normal) binary
– Signed
- Signed Magnitude
- 2’s complement
- 1’s complement*
- Excess-N*
- Floating Point
– For very large and small (fractional) numbers
- Codes
– Text
- ASCII / Unicode
– Decimal Codes
- BCD (Binary Coded Decimal)
/ (8421 Code)
* = Not covered in this class
28
Unsigned and Signed
- Normal (unsigned) binary can only represent
positive numbers
– All place values are positive
- To represent negative numbers we must use a
modified binary representation that takes into account sign (pos. or neg.)
– We call these signed representations
29
Signed Number Representation
- 2 Primary Systems
– Signed Magnitude – Two’s Complement (most widely used for integer
representation)
30
Signed numbers
- All systems used to represent
negative numbers split the possible binary combinations in half (half for positive numbers / half for negative numbers)
- In both signed magnitude and
2’s complement, positive and negative numbers are separated using the MSB
– MSB=1 means negative – MSB=0 means positive
0000 0001 0010 0011 0100 0101 0110 0111 1000 1111 1110 1101 1100 1011 1010 1001
+
31
Signed Magnitude System
- Use binary place values but now MSB represents the
sign (1 if negative, 0 if positive)
1 2 4 8
4-bit Unsigned 4-bit Signed Magnitude 0 to 15
Bit Bit 1 Bit 2 Bit 3 1 2 4 +/-
- 7 to +7
Bit Bit 1 Bit 2 Bit 3
8-bit Signed Magnitude
16 32 64 +/-
- 127 to +127
Bit 4 Bit 5 Bit 6 Bit 7 1 2 4 8 Bit Bit 1 Bit 2 Bit 3
32
Signed Magnitude Examples
4-bit Signed Magnitude
1 2 4 +/-
= -5
8-bit Signed Magnitude
1 1 1
1 2 4 +/-
= +3 1 1
16 32 64 +/- 1 2 4 8
Notice that +3 in signed magnitude is the same as in the unsigned system
1 2 4 +/-
= -7 1 1 1 1 1 1 1 1 = -19
16 32 64 +/- 1 2 4 8
1 1 1 = +25
Important: Positive numbers have the same representation in signed magnitude as in normal unsigned binary
33
Signed Magnitude Range
- Given n bits…
– MSB is sign – Other n-1 bits = normal unsigned place values
- Range with n-1 unsigned bits = [0 to 2n-1-1]
Range with n-bits of Signed Magnitude [ -2n-1 –1 to +2n-1–1]
34
Disadvantages of Signed Magnitude
- 1. Wastes a combination to represent -0
0000 = 1000 = 010
- 2. Addition and subtraction algorithms for signed
magnitude are different than unsigned binary (we’d like them to be the same to use same HW)
4
- 6
Swap & make res. negative
6
- 4
35
2’s Complement System
- Normal binary place values except MSB has negative
weight
– MSB of 1 = -2n-1
1 2 4 8
4-bit Unsigned 4-bit 2’s complement 0 to 15
Bit Bit 1 Bit 2 Bit 3 1 2 4
- 8
- 8 to +7
Bit Bit 1 Bit 2 Bit 3
8-bit 2’s complement
16 32 64
- 128
- 128 to +127
Bit 4 Bit 5 Bit 6 Bit 7 1 2 4 8 Bit Bit 1 Bit 2 Bit 3
36
2’s Complement Examples
4-bit 2’s complement
1 2 4
- 8
= -5
8-bit 2’s complement
1 1 1
1 2 4
- 8
= +3 1 1
16 32 64
- 128
1 2 4 8
Notice that +3 in 2’s
- comp. is the same as
in the unsigned system
1 2 4
- 8
= -1 1 1 1 1 1 1 = -127
16 32 64
- 128
1 2 4 8
1 1 1 = +25
Important: Positive numbers have the same representation in 2’s complement as in normal unsigned binary
37
2’s Complement Range
- Given n bits…
– Max positive value = 011…11
- Includes all n-1 positive place values
– Max negative value = 100…00
- Includes only the negative MSB place value
Range with n-bits of 2’s complement [ -2n-1 to +2n-1–1]
– Side note – What decimal value is 111…11?
- -110
38
Comparison of Systems
0000 0001 0010 0011 0100 0101 0110 0111 1000 1111 1110 1101 1100 1011 1010 1001
+1 +2 +3 +4 +5 +6 +7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Signed Mag. 2’s comp.
+1 +2 +3 +4 +5 +6 +7
- 8
- 7
- 6
- 5
- 4
- 3
- 2 -1
39
Unsigned and Signed Variables
- In C, unsigned variables use unsigned binary (normal
power-of-2 place values) to represent numbers
- In C, signed variables use the 2’s complement system
(Neg. MSB weight) to represent numbers
128 64 32 16 8 4 2 1 1 1 1 1 = +147
- 128
64 32 16 8 4 2 1 1 1 1 1 = -109
40
IMPORTANT NOTE
- All computer systems use the 2's complement
system to represent signed integers!
- So from now on, if we say an integer is signed,
we are actually saying it uses the 2's complement system unless otherwise specified
– We will not use "signed magnitude" unless explicitly indicated
41
Zero and Sign Extension
2’s complement = Sign Extension (Replicate sign bit): Unsigned = Zero Extension (Always add leading 0’s): 111011 = 00111011 011010 = 00011010 110011 = 11110011 pos. neg.
Increase a 6-bit number to 8-bit number by zero extending Sign bit is just repeated as many times as necessary
- Extension is the process of increasing the number of bits used
to represent a number without changing its value
42
Zero and Sign Truncation
- Truncation is the process of decreasing the number of bits used
to represent a number without changing its value 2’s complement = Sign Truncation (Remove copies of sign bit): Unsigned = Zero Truncation (Remove leading 0’s): 00111011 = 111011 00011010 = 011010 11110011 = 10011 pos. neg.
Decrease an 8-bit number to 6-bit number by truncating 0’s. Can’t remove a ‘1’ because value is changed Any copies of the MSB can be removed without changing the numbers value. Be careful not to change the sign by cutting off ALL the sign bits.
43
Data Representation
- In C/C++ variables can be of different types and sizes
– Integer Types (signed and unsigned) – Floating Point Types
C Type Bytes Bits MIPS Name ATmega328 [unsigned] char 1 8 byte byte [unsigned] short [int] 2 16 half-word word [unsigned] long [int] 4 32 word
- 1
[unsigned] long long [int] 8 64 double-word
- 1
C Type Bytes Bits MIPS Name ATmega328 float 4 32 single N/A double 8 64 double N/A
1Can emulate but has no single-instruction support
44
SHORTHAND FOR BINARY
Hexadecimal and Octal
45
Binary, Octal, and Hexadecimal
- Octal (base 8 = 23)
- 1 Octal digit ( _ )8 can
represent: 0 – 7
- 3 bits of binary (_ _ _)2
can represent: 000-111 = 0 – 7
- Conclusion…
1 Octal digit = 3 bits
- Hex (base 16=24)
- 1 Hex digit ( _ )16 can
represent: 0-F (0-15)
- 4 bits of binary
(_ _ _ _)2 can represent: 0000-1111= 0-15
- Conclusion…
1 Hex digit = 4 bits
46
Binary to Octal or Hex
- Make groups of 3 bits
starting from radix point and working outward
- Add 0’s where necessary
- Convert each group of 3
to an octal digit
101001110.11 101001110.11 0
- Make groups of 4 bits
starting from radix point and working outward
- Add 0’s where
necessary
- Convert each group of 4
to an octal digit 516.68 14E.C16 5 1 6 6 1 4 E C
47
Octal or Hex to Binary
- Expand each octal digit
to a group of 3 bits
- Expand each hex digit
to a group of 4 bits 317.28 D93.816 011001111.0102 110110010011.10002 11001111.012 110110010011.12
48
Hexadecimal Representation
- Since values in modern computers are many bits, we
use hexadecimal as a shorthand notation (4 bits = 1 hex digit)
– 11010010 = D2 hex or 0xD2 if you write it in C/C++ – 0111011011001011 = 76CB hex or 0x76CB if you write it in C/C++
- Important Point: To interpret the value of a hex
number, you must know what underlying binary system is assumed (unsigned, 2’s comp. etc.)
– D2 hex (what if underlying binary system is unsigned?) – D2 hex (what if underlying binary system is signed?) – D2 hex ( = 'Ò' in Arial Unicode font)
49
BINARY CODES
ASCII & Unicode
50
Binary Representation Systems
- Integer Systems
– Unsigned
- Unsigned (Normal) binary
– Signed
- Signed Magnitude
- 2’s complement
- 1’s complement*
- Excess-N*
- Floating Point
– For very large and small (fractional) numbers
- Codes
– Text
- ASCII / Unicode
– Decimal Codes
- BCD (Binary Coded Decimal)
/ (8421 Code)
* = Not covered in this class
51
Binary Codes
- Using binary we can represent any kind of
information by coming up with a code
- Using n bits we can represent 2n distinct items
Colors of the rainbow:
- Red = 000
- Orange = 001
- Yellow = 010
- Green = 100
- Blue = 101
- Purple = 111
Letters:
- ‘A’ = 00000
- ‘B’ = 00001
- ‘C’ = 00010
. . .
- ‘Z’ = 11001
52
BCD
- Rather than convert a decimal number to binary which may lose
some precision (i.e. 0.110 = infinite binary fraction), BCD represents each decimal digit as a separate group of bits (exact decimal precision)
– Each digits is represented as a separate 4-bit number (using place values 8,4,2,1 for each dec. digit) – Often used in financial and other applications where decimal precision is needed
(439)10
0100 0011 1001
BCD Representation:
This is the Binary Coded Decimal (BCD) representation of 439
Important: Some processors have specific instructions to operate on #’s represented in BCD Unsigned Binary Rep.:
1101101112
This is the binary representation of 439 (i.e. using power of 2 place values)
53
ASCII Code
- Used for representing text characters
- Originally 7-bits but usually stored as 8-bits = 1-
byte in a computer
- Example:
– printf(“Hello\n”); – Each character is converted to ASCII equivalent
- ‘H’ = 0x48, ‘e’ = 0x65, …
- \n = newline character is represented by either one or two ASCII
character
– LF (0x0A) = line feed (moves cursor down a line) – CR (0x0D) = carriage return character (moves cursor to start of current line) – Newline for Unix / Mac = LF only – Newline for Windows = CR + LF
printf() is the method to print text to the screen in C
54
ASCII Table
LSD/MSD 1 2 3 4 5 6 7 NULL DLW SPACE @ P ` p 1 SOH DC1 ! 1 A Q a q 2 STX DC2 “ 2 B R b r 3 ETX DC3 # 3 C S c s 4 EOT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v 7 BEL ETB ‘ 7 G W g w 8 BS CAN ( 8 H X h x 9 TAB EM ) 9 I Y i y A LF SUB * : J Z j z B VT ESC + ; K [ k { C FF FS , < L \ l | D CR GS
- =
M ] m } E SO RS . > N ^ n ~ F SI US / ? O _
- DEL
55
UniCode
- ASCII can represent only the English
alphabet, decimal digits, and punctuation
– 7-bit code => 27 = 128 characters – It would be nice to have one code that represented more alphabets/characters for common languages used around the world
- Unicode
– 16-bit Code => 65,536 characters – Represents many languages alphabets and characters – Used by Java as standard character code
Unicode hex value (i.e. FB52 => 1111101101010010)