sign magnitude 4 bit two s complement signed integer
play

!!! sign-magnitude (4-bit) two's complement signed integer - PowerPoint PPT Presentation

(4-bit) unsigned integer representation modular arithmetic, overflow 15 0 13 1101 11 1011 14 1 1111 0000 1 0 1 1 + 5 + 0101 1110 0001 = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 + 2 + 0010 13 2 1101 0010 12 3 4-bit 1100


  1. (4-bit) unsigned integer representation modular arithmetic, overflow 15 0 13 1101 11 1011 14 1 1111 0000 1 0 1 1 + 5 + 0101 1110 0001 = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 + 2 + 0010 13 2 1101 0010 12 3 4-bit 1100 0011 8 4 2 1 weight unsigned 2 3 2 2 2 1 2 0 1011 integers 0100 11 4 position 3 2 1 0 1010 0101 10 5 1001 0110 1000 0111 9 6 n -bit unsigned integers: 8 7 x+y in n -bit unsigned arithmetic is (x + y) mod 2 N in math minimum = unsigned overflow = "wrong" answer = wrap-around = carry 1 out of MSB = math answer too big to fit maximum = Unsigned addition overflows if and only if a carry bit is dropped. 3 4 !!! sign-magnitude (4-bit) two's complement signed integer representation Most-significant bit (MSB) is sign bit 0 means non-negative 1 means negative 1 0 1 1 = 1 x -2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 Remaining bits are an unsigned magnitude -2 3 2 2 2 1 2 0 8-bit sign-magnitude: Anything weird here? 0 0000000 represents _____ Arithmetic? Example: 0 1111111 represents _____ 4 -bit two's complement integers: 4 - 3 != 4 + (-3) minimum = 1 0000101 represents _____ 00000100 +10000011 1 0000000 represents _____ maximum = ex Zero? 6 8

  2. ex 8-bit representations two’s complement vs. unsigned 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 _ _ …_ _ _ unsigned places 2 n -1 2 n -2 … 2 2 2 1 2 0 - 2 n -1 2 n -2 … 2 2 2 1 2 0 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 numbers: 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 2 0010 1110 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 1 x -2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 + 3 + 0011 + -3 + 1101 11 -5 difference = ___ = 2 ___ – 1 0 – 2 + 1 1111 0000 1110 0001 – 1 0 15 0 – 3 + 2 1101 0010 14 1 – 2 + 1 1111 0000 1111 0000 – 4 + 3 1100 0011 1110 0001 1110 0001 13 2 – 3 + 2 2 0010 -2 1110 1011 0100 1101 0010 1101 0010 – 5 + 4 – 4 4-bit + 3 12 3 1010 0101 1100 0011 1100 0011 4-bit + -3 + 1101 + 3 + 0011 – 6 + 5 two's 1001 0110 unsigned 1011 complement 0100 1000 0111 1011 0100 – 7 + 6 11 4 – 5 + 4 – 8 + 7 1010 0101 1010 0101 10 5 – 6 + 5 1001 0110 1001 0110 1000 0111 Modular Arithmetic 1000 0111 9 6 – 7 + 6 8 7 – 8 + 7 11 12

  3. two’s complement overflow A few reasons two’s complement is awesome Addition overflows Addition, subtraction, hardware if and only if the arguments have the same sign but the result does not. if and only if the carry in and carry out of the sign bit differ. Sign – 1 0 -1 1111 – 2 + 1 1111 0000 + 2 + 0010 1110 0001 – 3 + 2 Negative one 1101 0010 – 4 + 3 1100 0011 Complement rules 1011 0100 – 5 + 4 1010 0101 – 6 + 5 1001 0110 0110 6 1000 0111 – 7 + 6 + 0011 + 3 – 8 + 7 Modular Arithmetic Some CPUs/languages raise exceptions on overflow. C and Java cruise along silently... Feature? Oops? 13 ex Another derivation Convert/cast signed number to larger type. How should we represent 8-bit negatives? • For all positive integers x , 0 0 0 0 0 0 1 0 8-bit 2 we want the representations of x and –x to sum to zero. • We want to use the standard addition algorithm. _ _ _ _ _ _ _ _ 0 0 0 0 0 0 1 0 16-bit 2 00000001 00000010 00000011 + + + 00000000 00000000 00000000 1 1 1 1 1 1 0 0 8-bit -4 • Find a rule to represent –x where that works… _ _ _ _ _ _ _ _ 1 1 1 1 1 1 0 0 16-bit -4 Rule/name? 16 18

  4. unsigned shifting and arithmetic two's complement shifting and arithmetic signed unsigned x*2 n mod 2 w x*2 n mod 2 w f ( ) 0 0 0 1 1 0 1 1 1 0 0 1 1 0 1 1 x = 27; x = -101; y = x << 2; y = x << 2; logical shift left logical shift left 0 0 0 1 1 0 1 1 0 0 1 0 0 1 1 0 1 1 0 0 y == 108 y == 108 signed unsigned 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 1 x = -19; x/2 n ⎣ x/2 n x = 237; ⎦ y = x >> 2; y = x >> 2; arithmetic shift right logical shift right 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1 y == -5 y == 59 20 21 ex ex shift- and -add What does this function compute? Available operations unsigned puzzle(unsigned x, unsigned y) { implements x * 2 k x << k 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend