<<

(4-bit) unsigned representation modular , overflow 15 0 11 1011 14 1111 0000 1 13 1101 1110 0001 3 2 1 0 13 2 = 1 x 2 + 0 x 2 + 1 x 2 + 1 x 2 + 2 + 0010 1101 0010 + 5 + 0101 1 0 1 1 12 3 1100 4-bit 0011 8 4 2 1 weight unsigned 3 2 1 0 2 2 2 2 11 1011 0100 4 3 2 1 0 position 1010 0101 10 1001 0110 5 9 1000 0111 6 n-bit unsigned integers: 8 7 x+y in n-bit unsigned arithmetic is (x + y) mod 2N in math minimum = unsigned overflow = "wrong" answer = wrap-around = carry 1 out of MSB = math answer too big to fit maximum = Unsigned overflows if and only if a carry bit is dropped. 3 4

-magnitude !!! (4-bit) two's Most-significant bit (MSB) is sign bit signed integer representation 0 means non-negative 1 means negative 3 2 1 0 Remaining bits are an unsigned magnitude 1 0 1 1 = 1 x -2 + 0 x 2 + 1 x 2 + 1 x 2 -23 22 21 20 8-bit sign-magnitude: Anything weird here? 00000000 represents _____ Arithmetic? 01111111 represents _____ Example: 4 - 3 != 4 + (-3) 4-bit two's complement integers: 10000101 represents _____ minimum = 00000100 10000000 represents _____ +10000011 maximum = ex Zero? 6 8 8-bit representations ex two’s complement vs. unsigned _ _ …_ _ _ unsigned 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 places 2n-1 2n-2 … 22 21 20 -2n-1 2n-2 … 22 21 20 two's complement What's the difference? places 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1

n-bit two's complement : n-bit minimum = n-bit maximum = minimum = maximum = 9 10

4-bit unsigned vs. 4-bit two’s complement two’s complement addition

1 0 1 1 2 0010 -2 1110 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 1 x -23 + 0 x 22 + 1 x 21 + 1 x 20 + 3 + 0011 + -3 + 1101

___ 11 difference = ___ = 2 -5 – 1 0 – 2 1111 0000 + 1 15 0 – 1 0 – 3 1110 0001 + 2 14 1 – 2 + 1 1101 0010 1111 0000 1111 0000 – 4 + 3 1100 0011 13 1110 0001 2 – 3 1110 0001 + 2 1101 0010 1101 0010 – 5 1011 0100+ 4 12 3 – 4 + 3 -2 1110 2 0010 1010 0101 1100 0011 1100 4-bit 0011 4-bit two's – 6 1001 0110 + 5 unsigned + 3 + 0011 + -3 + 1101 1000 0111 11 1011 0100 4 – 5 1011 complement 0100 + 4 – 7 + 6 1010 0101 1010 0101 – 8 + 7 10 1001 0110 5 – 6 1001 0110 + 5 9 1000 0111 6 – 7 1000 0111 + 6 8 7 – 8 + 7 11 12 two’s complement overflow A few reasons two’s complement is awesome Addition overflows if and only if the arguments have the same sign but the result does not. Addition, , hardware if and only if the carry in and carry out of the sign bit differ.

– 1 0 Sign -1 1111 – 2 1111 0000 + 1 – 3 1110 0001 + 2 + 2 + 0010 1101 0010 Negative one – 4 + 3 1100 0011

– 5 1011 0100 + 4 Complement rules 1010 0101 – 6 1001 0110 + 5 6 0110 – 7 1000 0111 + 6 + 3 + 0011 – 8 + 7 Modular Arithmetic Some CPUs/languages raise exceptions on overflow. and Java cruise along silently... Feature? Oops? 13

ex Another derivation Convert/cast signed to larger type. How should we represent 8-bit negatives? • For all positive integers x, we want the representations of x and –x to sum to zero. 0 0 0 0 0 0 1 0 8-bit 2 • We want to use the standard addition . 16-bit 2 00000001 00000010 00000011 ______0 0 0 0 0 0 1 0 + + + 00000000 00000000 00000000

• Find a rule to represent –x where that works… 1 1 1 1 1 1 0 0 8-bit -4

______1 1 1 1 1 1 0 0 16-bit -4

Rule/name? 16 18 unsigned shifting and arithmetic two's complement shifting and arithmetic

signed unsigned n w n w x = 27; 0 0 0 1 1 0 1 1 x*2 mod 2 x = -101; 1 0 0 1 1 0 1 1 f( )x*2 mod 2

y = x << 2; logical shift left y = x << 2; logical shift left y == 108 0 0 0 1 1 0 1 1 0 0 y == 108 1 0 0 1 1 0 1 1 0 0

unsigned signed n 1 1 1 0 1 1 0 1 x = 237; n 1 1 1 0 1 1 0 1 x = -19; x/2 ⎣x/2 ⎦ y = x >> 2; logical shift right y = x >> 2; arithmetic shift right 0 0 1 1 1 0 1 1 0 1 y == 59 1 1 1 1 1 0 1 1 0 1 y == -5

20 21

shift-and-add ex What does this compute? ex

Available operations unsigned puzzle(unsigned x, unsigned y) { x << k implements x * 2k unsigned result = 0; x + y for (unsigned i = 0; i < 32; i++){ if (y & (1 << i)) { Implement y = x * 24 using only <<, +, and integer literals result = result + (x << i); } } return result; }

22 23