Lecture Slides 02 022-Integers
Lecture Slides 02 022-Integers
of Washington
Integers
University
of
Washington
Unsigned
Integers
¢ Unsigned
values
are
just
what
you
expect
§ b7b6b5b4b3b2b1b0
=
b727
+
b626
+
b525
+
…
+
b121
+
b020
§ Useful
formula:
1+2+4+8+...+2N-‐1
=
2N
-‐1
¢ You
add/subtract
them
using
the
normal
“carry/borrow”
rules,
just
in
binary
63 00111111
+ 8 +00001000
71 01000111
Integers
University
of
Washington
Signed
Integers
¢ Let's
do
the
natural
thing
for
the
posi.ves
§ They
correspond
to
the
unsigned
integers
of
the
same
value
§ Example
(8
bits):
0x00
=
0,
0x01
=
1,
…,
0x7F
=
127
¢ But,
we
need
to
let
about
half
of
them
be
nega.ve
§ Use
the
high
order
bit
to
indicate
nega%ve:
call
it
the
“sign
bit”
Call
this
a
“sign-‐and-‐magnitude”
representaQon
§
§ Examples
(8
bits):
§ 0x00
=
000000002
is
non-‐negaQve,
because
the
sign
bit
is
0
§ 0x7F
=
011111112
is
non-‐negaQve
§ 0x85
=
100001012
is
negaQve
§ 0x80
=
100000002
is
negaQve…
Integers
University
of
Washington
Sign-‐and-‐Magnitude
Nega.ves
¢ How
should
we
represent
-‐1
in
binary?
§ Sign-‐and-‐magnitude:
100000012
Use
the
MSB
for
+
or
-‐,
and
the
other
bits
to
give
magnitude
–7 +0
–6 1111 0000 +1
–5 1110 0001 +2
1101 0010
–4 +3
1100 0011
– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5
–1 1000 0111 +6
–0 +7
Integers
University
of
Washington
Sign-‐and-‐Magnitude
Nega.ves
¢ How
should
we
represent
-‐1
in
binary?
§ Sign-‐and-‐magnitude:
100000012
Use
the
MSB
for
+
or
-‐,
and
the
other
bits
to
give
magnitude
(Unfortunate
side
effect:
there
are
two
representaQons
of
0!)
–7 +0
–6 1111 0000 +1
–5 1110 0001 +2
1101 0010
–4 +3
1100 0011
– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5
–1 1000 0111 +6
–0 +7
Integers
University
of
Washington
Sign-‐and-‐Magnitude
Nega.ves
¢ How
should
we
represent
-‐1
in
binary?
§ Sign-‐and-‐magnitude:
100000012
Use
the
MSB
for
+
or
-‐,
and
the
other
bits
to
give
magnitude
(Unfortunate
side
effect:
there
are
two
representaQons
of
0!)
§ Another
problem:
math
is
cumbersome
§ Example:
–7 +0
–6 1111 0000 +1
4
-‐
3
!=
4
+
(-‐3)
–5 1110 0001 +2
1101 0010
0100 –4 +3
+1011 1100 0011
1111
– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5
–1 1000 0111 +6
–0 +7
Integers
University
of
Washington
–7 1000 0111 +6
Integers
–8 +7
University
of
Washington
Integers
University
of
Washington
Two’s
Complement
¢ Why
does
it
work?
§ Put
another
way:
given
the
bit
representaQon
of
a
posiQve
integer,
we
want
the
negaQve
bit
representaQon
to
always
sum
to
0
(ignoring
the
carry-‐out
bit)
when
added
to
the
posiQve
representaQon
§ This
turns
out
to
be
the
bitwise
complement
plus
one
§ What
should
the
8-‐bit
representaQon
of
-‐1
be?
00000001
+????????
(we
want
whichever
bit
string
gives
the
right
result)
00000000
00000010 00000011
+???????? +????????
00000000 00000000
Integers
University
of
Washington
Two’s
Complement
¢ Why
does
it
work?
§ Put
another
way:
given
the
bit
representaQon
of
a
posiQve
integer,
we
want
the
negaQve
bit
representaQon
to
always
sum
to
0
(ignoring
the
carry-‐out
bit)
when
added
to
the
posiQve
representaQon
§ This
turns
out
to
be
the
bitwise
complement
plus
one
§ What
should
the
8-‐bit
representaQon
of
-‐1
be?
00000001
+11111111
(we
want
whichever
bit
string
gives
the
right
result)
100000000
00000010 00000011
+???????? +????????
00000000 00000000
Integers
University
of
Washington
Two’s
Complement
¢ Why
does
it
work?
§ Put
another
way:
given
the
bit
representaQon
of
a
posiQve
integer,
we
want
the
negaQve
bit
representaQon
to
always
sum
to
0
(ignoring
the
carry-‐out
bit)
when
added
to
the
posiQve
representaQon
§ This
turns
out
to
be
the
bitwise
complement
plus
one
§ What
should
the
8-‐bit
representaQon
of
-‐1
be?
00000001
+11111111
(we
want
whichever
bit
string
gives
the
right
result)
100000000
00000010 00000011
+11111110 +11111101
100000000 100000000
Integers
University
of
Washington
Integers
University
of
Washington
Visualiza.ons
¢ Same
W
bits
interpreted
as
signed
vs.
unsigned:
Two’s" 2w" 2w" Two’s"
complement" complement"
+2w–1" 2w–1" 2w–1" +2w–1"
0" 0" 0" 0"
–2w–1" Unsigned" Unsigned"
–2w–1"
¢ Two’s
complement
(signed)
addi.on:
x
and
y
are
W
bits
wide
x + y!
+2w" Positive overflow"