Top Banner
© D. Thiebaut Computer Science — Smith College Number Systems CSC231 — Fall 2014 D. Thiebaut
29

Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Jul 06, 2018

Download

Documents

trandang
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

© D. Thiebaut Computer Science — Smith College

Number SystemsCSC231 — Fall 2014

D. Thiebaut

Page 2: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

ALU

CU

Processor

Page 3: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

ALU

bjbi

sum

carry

Page 4: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

yx

z

x0 y0x1 y1x2 y2xn yn x3 y3

z0z1z2z3zn

Page 5: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

x0y0

z0

0

+ 0 ____

= 0 0

0+ 1____

= 0 1

1+ 0____

= 0 1

1+ 1____

= 1 0

carry

Page 6: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

x0 y0 Carry y0

0 0 0 0

0 1 0 1

1 0 0 1

1 1 1 0

Page 7: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

x0 y0 Carry z0

0 0 0 0

0 1 0 1

1 0 0 1

1 1 1 0

Carry = bi and bj !Sum = bi xor bj

Page 8: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

x0

z0

XORAND

y0

carry

Page 9: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Moral of the Story: Addition is performed

by logic operations using natural binary

numbers…

(unsigned arithmetic)

Page 10: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

How can we represent signed binary numbers when all we have are bits (0/1)?

Whichever system we use should work with the ALU adder…

Page 11: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

4-bit Nybble

Binary Hex Unsigned Decimal

0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 81001 9 91010 A 101011 B 111100 C 121101 D 131110 E 141111 F 15

Page 12: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Binary Hex Unsigned Decimal

0 000 0 00 001 1 10 010 2 20 011 3 30 100 4 40 101 5 50 110 6 60 111 7 71 000 8 81 001 9 91 010 A 10 1 011 B 111 100 C 121 101 D 131 110 E 141 111 F 15

4-bit NybbleSign Bit

Page 13: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Binary Hex Unsigned Decimal

0 000 0 00 001 1 10 010 2 20 011 3 30 100 4 40 101 5 50 110 6 60 111 7 71 000 8 81 001 9 91 010 A 10 1 011 B 111 100 C 121 101 D 131 110 E 141 111 F 15

4-bit Nybble

Positive!Numbers

Negative!Numbers

Page 14: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Signed Magnitude!Number System

Page 15: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Signed Magnitude Rule: To find the opposite of a positive number,

just change its MSB to 1

0 101 (+5)

1 101(-5)

and vice versa…

Page 16: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Binary Hex Unsigned Decimal

Signed Magnitude

0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +3!0 100 4 4 +4!0 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -01 001 9 9 -11 010 A 10 -2 1 011 B 11 -31 100 C 12 -41 101 D 13 -51 110 E 14 -61 111 F 15 -7

4-bit Nybble

Page 17: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Does this System work With the ALU Adder?

Binary Hex Unsigned Decimal

Signed Magnitud

e0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +3!0 100 4 4 +4!0 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -01 001 9 9 -11 010 A 10 -2 1 011 B 11 -31 100 C 12 -41 101 D 13 -51 110 E 14 -61 111 F 15 -7

3 + -3 ——- = 0

4 + -1 ——- = 3

Page 18: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

1's Complement!Number System

Page 19: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

1's Complement Rule: To find the opposite of a positive number,

just flip all bits

0 101 (+5)

1 010(-5)

and vice versa…

Page 20: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Binary Hex Unsigned Decimal

1's!Complement

0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +3!0 100 4 4 +4!0 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -71 001 9 9 -61 010 A 10 -5! 1 011 B 11 -41 100 C 12 -31 101 D 13 -21 110 E 14 -11 111 F 15 -0

4-bit Nybble

Page 21: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Does this System work With the ALU Adder?

Binary Hex Unsigned Decimal

1's!Complement

0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +3!0 100 4 4 +4!0 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -71 001 9 9 -61 010 A 10 -5 1 011 B 11 -41 100 C 12 -31 101 D 13 -21 110 E 14 -11 111 F 15 -0

3 + -3 ——- = 0

4 + -1 ——- = 3

5 + -3 ——- = 2

Page 22: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

2's Complement!Number System

Page 23: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

2's Complement Rule: To find the opposite of a positive number,

just flip all bits, and add 1

0 101!(+5)

1 011(-5)

and vice versa…

1 010 + 1

0 100 + 1

Page 24: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Binary Hex Unsigned Decimal

2's!Complement

0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +3!0 100 4 4 +4!0 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -81 001 9 9 -71 010 A 10 -6! 1 011 B 11 -51 100 C 12 -41 101 D 13 -31 110 E 14 -21 111 F 15 -1

4-bit Nybble

Page 25: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Does this System work With the ALU Adder?

Binary Hex Unsigned Decimal

2's!Complement

0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +3!0 100 4 4 +4!0 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -81 001 9 9 -71 010 A 10 -6 1 011 B 11 -51 100 C 12 -41 101 D 13 -31 110 E 14 -31 111 F 15 -1

3 + -3 ——- = 0

4 + -1 ——- = 3

5 + -3 ——- = 2

Page 26: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Interesting Property

• What is the binary representation of -1 as a byte?

• What is the binary representation of -1 as a word?

• What is the binary representation of -1 as a dword?

Page 27: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

int x = 0x7fffffff - 5; for ( int i=0; i<10; i++ ) System.out.println( x++ );

Page 28: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Java ints are signed!

Page 29: Number Systems - Smith College · How can we represent signed binary numbers when all we have are bits (0/1)? Whichever system we use should work with the ALU adder…

Exercises