ECE 2560 Introduction to Microcontroller-Based Systems Lecture 3
Signed and Unsigned Numbers
ECE 2560 Introduction to Microcontroller-Based Systems –
Copyright By PowCoder代写 加微信 powcoder
Office Hours
Tentative time and space
• Tuesdays 1 pm – 2 pm
• Tuesdays 2 pm – 3 pm
• Thursdays 1 pm – 2 pm
Dreese 660 Dreese 331 Dreese 331
Will post Quiz #1 on Carmen today (or tomorrow)
ECE 2560 Introduction to Microcontroller-Based Systems –
n-bit Unsigned Numbers Unsigned number = positive number
8-bits unsigned numbers range from 0 to 255
n-bit unsigned numbers rangefrom0to2n –1
0000 0000 0
0000 0001 1 0000 0010 2
1111 1101 253 1111 1110 254 1111 1111 255
ECE 2560 Introduction to Microcontroller-Based Systems –
Signed Numbers – First Attempt
How do we represent negative integers in binary?
In decimal we represent negative numbers by prefixing them with a “–” sign (– 0110)2 = (– 6)10 does not work on computers!!
There is no (–) sign
But we could still use sign bit and magnitude
0 for positive 1 for negative
Difficult to add two numbers
• Check signs: if both signs are the same, add both numbers …
• … if not compare magnitudes: subtract smaller number from larger one …
• … decide on the sign of the result
ECE 2560 Introduction to Microcontroller-Based Systems –
Yikes! We need to do better!
Signed Numbers & Complements
The sign and magnitude method does not work well on computers
– at least not for integers or fixed point arithmetic
Modern computers use 2’s complement for signed numbers
Both 1’s and 2’s complement work only in context of a fixed word length
Two ingredients for complements:
1. n = word length in bits = size of the register
2. N = Binary number we want to complement
ECE 2560 Introduction to Microcontroller-Based Systems –
n = 8 bits
n-bit Ones’ Complement
n-bit 1’s complement of a binary number is obtained by flipping its bits
Given binary number N and register size n
• fill the register – i.e., zero pad the number as needed to have n bits • flipallbits–i.e.,swapa0witha1andviceversa
e.g. N = 101101 n = 8 bits Þ 8-bit ones’ complement N = 101101
8-bit ones’ complement
Same idea for n = 16 bits – only more bits to fill and toggle
ECE 2560 Introduction to Microcontroller-Based Systems –
Ones’ Complement Why is it called ones’ complement?
N = 101101
8-bit ones’ complement of N
n-bit ones’ complement of N
= 2n – 1 – N
ECE 2560 Introduction to Microcontroller-Based Systems –
Ones’ Complement What purpose does the one’s complement serve?
Þ Not much – at least in today’s computers
However, some earlier computers used 1’s complement for signed numbers
i.e., to express – 41 use 1’s complement of 41, N = 101001
Does it work?
Yes, it does.
But there are some issues
e.g., normally 41 + (– 41) = 0
with ones’ complement method
00101001 + 11010110 = 11111111
there are two representations of zero with ones’ complement
and 11111111
ECE 2560 Introduction to Microcontroller-Based Systems –
Two’s Complement
The 2’s complement of a binary number is obtained by adding 1 to its ones’ complement
Given binary number N and register size n
• fill the register – i.e., zero pad the number as needed to have n bits
• flip all bits – i.e., 0 ↔1 • add1
N = 101101
8-bit ones’ complement
Þ 8-bit two’s complement
ECE 2560 Introduction to Microcontroller-Based Systems –
2’s Complement – The Shortcut There is a shortcut to write the 2’s complement of a binary number Given binary number N and register size n
fill the register – i.e., zero pad the number as needed to have n bits
leave the least significant zeros and first 1 unchanged flip all remaining bits
N = 110100
Þ 8-bit two’s complement
leave unchanged
ECE 2560 Introduction to Microcontroller-Based Systems –
Two’s Complement
Why is it called two’s complement? N = 101101
8-bit two’s complement
Power of 2
Power of two’s complement
n-bit two’s complement of N
if N ≠ 0 ECE 2560 Introduction to Microcontroller-Based Systems –
Two’s Complement A better definition of two’s complement
n-bit two’s complement of N
2n–N ifN≠0 0 if N = 0
Compare to ones’ complement
n-bit ones’ complement of N
= 2n – 1 – N
Works for zero when restricted to n-bits
2’s complement = 1’s complement + 1
ECE 2560 Introduction to Microcontroller-Based Systems –
Signed Numbers w/ 2’s Complement
Use two’s complement representation for signed numbers
modern computers – including our MCU – use this method
• If a number N is positive, use binary representation of N
• If N is negative, use two’s complement of absolute value of N
e.g. +/- 43
2’s complement of |-43|
ECE 2560 Introduction to Microcontroller-Based Systems –
Does this work? Is this consistent with the rules of arithmetic?
• N+(–N)=0
Previous slide
We get the original N when we complement twice incrementing and decrementing
• Successors and predecessor relationships are consistent with
(1)10 =0001 (2)10 =0010 (– 1)10 = 1111 (– 2)10 = 1110
(3)10 =0011 (– 3)10 = 1101
1101 1110 1111 0000 0001 0010 0011
ECE 2560 Introduction to Microcontroller-Based Systems –
Signed Numbers
8-bits can represent 256 distinct values
8-bit unsigned numbers range from -128 to 127
n-bit signed numbers –2n-1 to2n-1 –1
not sign- magnitude
negative numbers start with a “1”
positive numbers start with a “0”
1000 0000 -128 1000 0001 -127
1111 1110 -2 1111 1111 -1 0000 0000 0 0000 0001 1
0111 1110 126 0111 1111 127
2’s complement
of the absolute value
ECE 2560 Introduction to Microcontroller-Based Systems –
binary representation of the number
Signed Numbers
Given 2’s complement signed numbers find the decimal values
• 0110 1001 • 1101 0001
Positive Number 105
Negative Number (11010001)2 = 209
• 0010 1010 • 1110 1110
(00101111)2 = 47 Positive Number 42
Negative Number – 18
2’s complement is 256 – 209 = 47 2’s complement of 11010001 is 00101111
ECE 2560 Introduction to Microcontroller-Based Systems –
Addition of Signed/Unsigned Numbers
Computers add all numbers using the same hardware – they do not distinguish between signed or unsigned numbers
Unsigned Number Interpretation
Signed Number Interpretation
overflow possible did not happen
00101011 + 00010010
overflow possible did not happen
ECE 2560 Introduction to Microcontroller-Based Systems –
Addition of Signed/Unsigned Numbers
Computers add all numbers using the same hardware – they do not distinguish between signed or unsigned numbers
Unsigned Number Interpretation
Signed Number Interpretation
+18 +00010010 +18
231 11100111 -25
overflow possible did not happen
overflow not possible! ECE 2560 Introduction to Microcontroller-Based Systems –
Addition of Signed/Unsigned Numbers
Computers add all numbers using the same hardware – they do not distinguish between signed or unsigned numbers
Unsigned Number Interpretation
43 + 238 281
Signed Number Interpretation
00101011 + 11101110
overflow not possible! carry out of “sign bit”
ECE 2560 Introduction to Microcontroller-Based Systems –
Addition of Signed/Unsigned Numbers
Computers add all numbers using the same hardware – they do not distinguish between signed or unsigned numbers
Unsigned Number Interpretation
213 + 238 451
11010101 + 11101110
Signed Number Interpretation
-43 + -18 -61
overflow possible did not happen
ECE 2560 Introduction to Microcontroller-Based Systems –
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com