Signed Binary Numbers

Download Report

Transcript Signed Binary Numbers

CSE 111
Representing Numeric Data in a
Computer
Slides adapted from Dr. Kris
Schindler
Unsigned Binary Numbers
Range: 02n-1
where n is the number of bits
Positional Notation
P
o
s
i
t
i
o
n
W
e
i
g
h
t
B
i
t
n
1
n
2
5
4
3
2
1
0
n
1
n
2
3
2
1
6
8
4
2
1
2
2
b
b
n
1
n
2
b
b
b
b
b
b
5
4
3
2
1
0
Example: 101100two
0
1
2
3
n

2
n

1
b

2

b

2

b

2

b

2



b

2

b

2
0
1
2
3
n

2
n

1
n

2
n

1
b

1

b

2

b

4

b

8



b

2

b

2
0
1
2
3
n

2
n

1
0

1

0

2

1

4

1

8

0

16

1

32

4

8

32

4
Unsigned Binary Numbers
How do we convert from a decimal number to
a binary number?
i / 2  q 0 , r0
b 0  r0
q 0 / 2  q 1 , r1
b 1  r1
q 1 / 2  q 2 , r2
b 2  r2
...
...
where
i  Decimal Number
b k  Binary Bit k
q x  Quotient
r  Re mainder
Binary
Number
Continue until q=0
 b n ... b 2 b 1 b 0
Unsigned Binary Numbers
How do we convert from a decimal number to
a binary number?
Example: 39ten
39 / 2  19 ,1
19 / 2  9 ,1
9 / 2  4 ,1
4 / 2  2 ,0
2 / 2  1, 0
1 / 2  0 ,1
39
ten
b0  1
b1  1
b2  1
b3  0
b4  0
b5  1
 100111
two
Bit Positions
MSB
Most Significant Bit
Leftmost Bit Position
LSB
Least Significant Bit
Rightmost Bit Position
Signed Binary Numbers
The most significant bit (leftmost) represents
the sign
Negative (-): 1
Positive (+): 0
Signed Binary Numbers
Computers represent signed numbers using two’s
complement notation
Signed Binary Numbers
Two’s Complement
Representation of a negative binary number
Consider an n-bit number, x
The two’s complement of the number is 2n - x
This process is called taking the two’s complement of a number
Taking the two’s complement of a number negates it
Signed Binary Numbers
Two’s Complement
Shortcut for taking the two’s complement of a number
Start at the least significant (rightmost) bit and move left (toward the
most significant bit)
Keep every bit until you reach the first 1
Keep that 1
Invert every bit (01,1  0) after the first 1 as you continue to
move left
Signed Binary Numbers
Two’s Complement
Examples:
-4
 Take the two’s complement of 4 (00000100)
 11111100 = -4
-9
 Take the two’s complement of 9 (00001001)
 11110111 = -9
Since the above are negative, taking the two’s complement will
allow you to determine the magnitude, which is the positive
equivalent
Signed Binary Numbers
Two’s Complement
Examples:
+6
 Since the number is positive, you don’t need to take the two’s
complement
 000000110 = +6
+18
 Since the number is positive, you don’t need to take the two’s
complement
 000010010 = +18
Signed Binary Numbers
Two’s Complement
Since taking the two’s complement of a number negates it,
taking the two’s complement twice gives you the original
number back
Example:
+12 is represented by 00001100
Taking the two’s complement results in -12 (11110100)
Taking the two's complement of -12 results in +12 (00001100)
Floating Point
Very large/small numbers
Fractions
Example
8.5 x
223
SE
x
p
o
n
e
n
t(
E
) S
i
g
n
i
f
i
c
a
n
d
(
F
)
1
8
2
3
B
i
t
s
S
(
E
b
i
a
s
)
(
1
)
X
1
.
F
X
2
100.12 x 223
Normalized
 1.0012 x 227
Exponent
 Bias = 127
 127+26 = 153 = 100110012
Significand: 00100000000000000000000
Sign: 0
Number: 01001000100100000000000000000000
References
 J.
Glenn Brookshear, Computer Science - An
Overview, 11th edition, Addison-Wesley as an
imprint of Pearson, 2012
Donald D. Givone, Digital Principles and
Design, McGraw-Hill, 2003
John L. Hennessy and David A. Patterson,
Computer Organization and Design, The
Hardware/Software Interface, 3rd Edition,
Morgan Kaufmann Publishers, Inc., 2005