You are on page 1of 14

Lecture 7

Integer Number Representation and Binary Arithmetic


4.1

Positional Number Systems


Grouping Systems: Define a unit symbol. Continue to define the alphabet by defining group symbols that contain the unit symbol. Examples: Coins, Roman numerals. Problems: The representation of large quantities is awkward and arithmetic operations arent straightforward. Positional notation is a great alternative. What defines a positional system: (1) The radix or base (b). (2) Alphabet: A set of symbols with number of elements equal to the base. (3) Representation:

d n 1

d 2 d1d 0 = d n 1b n 1 +

+ d 2b 2 + d1b1 + d 0b 0
4.2

Conversion Between Bases


From base 10 to base b:
Successive divisions. Each new division finds the multiplier for an exponent of the base thats one plus the previous exponent. Result comes from reading the remainders in reverse.

From base b to base 10:


Compute the representation equation

x10 = d n 1

d 2 d1d 0 = d n 1b n 1 +

+ d 2b 2 + d1b1 + d 0b 0
4.3

Favorite Bases in CS37


Decimal: b=10, alphabet={0,1,2,3,4,5,6,7,8,9} Binary: b=2, alphabet={0,1} Octal: b=8, alphabet={0,1,2,3,4,5,6,7} Hexadecimal: b=16, alphabet={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
4.4

Representing Numbers in Different Bases

29710
Base 2: Base 8: Base 16:
4.5

Conversion Shortcuts from b=2


From binary to octal: Each group of three binary digits gives an octal digit.
28 27 26 25 2 4 23 2 2 2120

82

81

80

From binary to hexadecimal: Each group of four digits gives a hexadecimal digit.
2 7 26 25 2 4 23 2 2 2120 161

160

4.6

Exercise

29710 = 1001010012

2978 = 29716 =
Remember that padding a number with 0s on the left does not change the value it represents.
4.7

Integer Representation
(1) Signed magnitude
rep( x) = 0 bin( x) if 2 n-1 > x 0 1 bin( x ) if -2 n-1 < x < 0

(2) 1s Complement
rep( x) = bin( x) if 2 n-1 > x 0 bin((2 n 1) x ) if -2 n-1 < x < 0

(3) 2s Complement
rep( x) = bin( x) if 2 n-1 > x 0 bin( 2 n x ) if -2 n-1 < x < 0
4.8

Integer Representation
(4) Excess (or biased)
First, define a bias B, a value that when added to the number youre going to represent results in a nonnegative number less than 2 n (n is the number of bits you have in your representation).

rep( x) = bin( x + B) ( B x < 2 n B)


Usually, the bias is chosen to be a value that falls roughly in the middle of the scale of values allowed by the n 1 n 1 representation, such as 2 or ( 2 1) .
4.9

What You Can Do with 4 bits


Decimal
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Binary
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Unsigned integer
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Signed Magnitude
+0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7

1s complement
0 1 2 3 4 5 6 7 -7 -6 -5 -4 -3 -2 -1 0

2s complement
0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1

Biased

4.10

10

Operations on Integers
A non-exhaustive list

1) Absolute Value 2) Addition 3) Subtraction 4) Multiplication 5) Division

6) AND 7) OR 8) NOT 9) Shift-left 10) Shift-right

  
4.11

 

11

Operations on 2s Complement
Absolute value: Look at the leftmost bit, if 0,
youre done, if 1, you have to compute its 2s complement.

Addition: Start adding the bits of the two operands,


from the rightmost position, carrying over any excess to the position immediately to the left. Youre done when you have added all the bits in the representation. Note that overflow can occur:

C = A + B, A 0, B 0 if C < 0, overflow C = A + B, A < 0, B < 0 if C 0, overflow


4.12

12

The 1-bit Adder Circuit (half-adder)


A
A B

A B

Sum

CarryOut

4.13

13

The 1-bit Adder Circuit (full-adder)


CarryIn A
A B

A B

+
CarryIn

Sum

CarryOut

b CarryOut

4.14

14

You might also like