AIT CS 04 Cont
AIT CS 04 Cont
For positive (unsigned) integers, there is a 1-to-1 relationship between the decimal
representation of a number and its binary representation. If you have a 4-bit
number, there are 16 possible combinations, and the unsigned numbers go from 0
to 15:
0b0000 = 0 0b0001 = 1 0b0010 = 2 0b0011 = 3
0b0100 = 4 0b0101 = 5 0b0110 = 6 0b0111 = 7
0b1000 = 8 0b1001 = 9 0b1010 = 10 0b1011 = 11
0b1100 = 12 0b1101 = 13 0b1110 = 14 0b1111 = 15
0110
positive 6
1011
negative 3
Sign Magnitude Representation
0000
positive 0
1000
negative 0
Sign Magnitude Representation
1 000 = -0 0 000 = 0
1 001 = -1 0 001 = 1
1 010 = -2 0 010 = 2
1 011 = -3 0 011 = 3
1 100 = -4 0 100 = 4
1 101 = -5 0 101 = 5
1 110 = -6 0 110 = 6
1 111 = -7 0 111 = 7
Can we do better?
Now Lets Try a Better Approach!
A Better Idea
• Ideally, binary addition would just work regardless of whether the number is
positive or negative.
0101
+????
0000
A Better Idea
• Ideally, binary addition would just work regardless of whether the number is
positive or negative.
0101
+1011
0000
A Better Idea
• Ideally, binary addition would just work regardless of whether the number is
positive or negative.
0011
+????
0000
A Better Idea
• Ideally, binary addition would just work regardless of whether the number is
positive or negative.
0011
+1101
0000
A Better Idea
• Ideally, binary addition would just work regardless of whether the number is
positive or negative.
0000
+????
0000
A Better Idea
• Ideally, binary addition would just work regardless of whether the number is
positive or negative.
0000
+0000
0000
There Seems Like a Pattern Here…
A binary number plus its inverse is all 1s. Add 1 to this to carry over all 1s and get 0!
0101 1111
+1010 +0001
1111 0000
Another Trick
• To find the negative equivalent of a number, work right-to-left and write down
all digits through when you reach a 1. Then, invert the rest of the digits.
100100
+??????
000000
Another Trick
• To find the negative equivalent of a number, work right-to-left and write down
all digits through when you reach a 1. Then, invert the rest of the digits.
100100
+???100
000000
Another Trick
• To find the negative equivalent of a number, work right-to-left and write down
all digits through when you reach a 1. Then, invert the rest of the digits.
100100
+ 011100
000000
Two’s Complement
Two’s Complement
• In two’s complement, we represent a
positive number as itself, and its
negative equivalent as the two’s
complement of itself.
• The two’s complement of a number is
the binary digits inverted, plus 1.
• This works to convert from positive to
negative, and back from negative to
positive!
Two’s Complement
• Con: more difficult to represent, and
difficult to convert to/from decimal and
between positive and negative.
• Pro: only 1 representation for 0!
• Pro: all bits are used to represent as
many numbers as possible
• Pro: the most significant bit still indicates
the sign of a number.
• Pro: addition works for any combination
of positive and negative!
Two’s Complement
• Adding two numbers is just…adding! There is no special case needed for
negatives. E.g. what is 2 + -5?
0010 2
+1011 -5
1101 -3
Two’s Complement
• Subtracting two numbers is just performing the two’s complement on one of
them and then adding. E.g. 4 – 5 = -1.
0100 4 0100 4
- 0101 5 +1011 -5
1111 -1
How to Read Two’s Complement #s
• Multiply the most significant bit by -1 and multiply all the other bits by 1 as normal
1 1
_ _ 1
_ 0
_
23 22 21 20
= 1*-8 + 1*4 + 1*2 + 0*1 = -2
How to Read Two’s Complement #s
• Multiply the most significant bit by -1 and multiply all the other bits by 1 as normal
0 1
_ _ 1
_ 0
_
23 22 21 20
= 0*-8 + 1*4 + 1*2 + 0*1 = 6
Practice: Two’s Complement
What are the negative or positive equivalents of the numbers below?
a) -4 (1100)
b) 7 (0111)
c) 3 (0011)
Practice: Two’s Complement
What are the negative or positive equivalents of the numbers below?
a) -4 (1100) -> 4 (0100)
b) 7 (0111) -> (1001)
c) 3 (0011) -> (1101)
Some Extra Slides for Review
Two's Complement
In practice, a negative number in two's
complement is obtained by inverting all
the bits of its positive counterpart*, and
then adding 1, or: x = ~x + 1
Example: The number 2 is represented as normal in
binary: 0010
0010 ☞ 1101
1101
+ 1
1110
1011 ☞ 0100
0100
+ 1
0101
0010 ☞ 2
+1011 ☞ -5
1101 ☞ -3 decimal (wow!)
Two's Complement: Neat Properties
More useful properties:
0100 ☞ 4, 0101 ☞ 5
Find the two's complement of 5: 1011
add:
0100 ☞ 4
+1011 ☞ -5
1111 ☞ -1 decimal
Practice
Convert the following 4-bit numbers
from positive to negative, or from
negative to positive using two's
complement notation:
a. -4 (1100) ☞
b. 7 (0111) ☞
c. 3 (0011) ☞
d. -8 (1000) ☞
Practice
Convert the following 4-bit numbers
from positive to negative, or from
negative to positive using two's
complement notation:
a. -4 (1100) ☞ 0100
b. 7 (0111) ☞ 1001
c. 3 (0011) ☞ 1101
a. -4 (11111100) ☞ 00000100
b. 27 (00011011) ☞ 11100101
d. 1 (00000001) ☞ 11111111
History: Two’s complement
• The binary representation was first proposed by John
von Neumann in First Draft of a Report on the EDVAC
(1945)
• That same year, he also invented the merge sort algorithm
11 5
10 6
9 8 7
127
Overflow
• What is happening here? Assume 4-bit numbers.
0b1101
+ 0b0100 15 0 1
14 2
13 3
12 4
Signed Unsigned 11 5
10 6
-3 + 4 = 1 13 + 4 = 1 9 8 7
No overflow Overflow
Overflow in Signed Addition
Signed overflow wraps around to the negative numbers:
YouTube fell into this trap — their view counter was a signed, 32-bit int. They
fixed it after it was noticed, but for a while, the view count for Gangnam Style
(the first video with over INT_MAX number of views) was negative.
Overflow In Practice: PSY
https://arstechnica.com/gadgets/2022/01/google-fixes-nightmare-android-bug-
that-stopped-user-from-calling-911/
Overflow In Practice: Timestamps
• Many systems store timestamps as the number of seconds since Jan. 1, 1970
in a signed 32-bit integer.
• Problem: the latest timestamp that can be represented this way is 3:14:07 UTC
on Jan. 13 2038!
Overflow In Practice: Gandhi
• In the game “Civilization”, each
civilization leader had an
“aggression” rating. Gandhi was
meant to be peaceful, and had a
score of 1.
• If you adopted “democracy”, all
players’ aggression reduced by 2.
Gandhi’s went from 1 to 255!
• Gandhi then became a big fan of
nuclear weapons.
https://kotaku.com/why-gandhi-is-such-an-asshole-in-civilization-1653818245
148
Overflow in Practice:
• Pacman Level 256
• Make sure to reboot Boeing Dreamliners every 248 days
• Comair/Delta airline had to cancel thousands of flights days before Christmas
• Reported vulnerability CVE-2019-3857 in libssh2 may allow a hacker to
remotely execute code
• Donkey Kong Kill Screen