Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier.

Similar presentations


Presentation on theme: "Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier."— Presentation transcript:

1 Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier

2 Recall from Last Time Unsigned integer representation and mathematics.

3 Unsigned Addition Standard Addition Function – Ignores carry output Implements Modular Arithmetic s= UAdd w (u, v)=u + v mod 2 w u v + u + v True Sum: w+1 bits Operands: w bits Discard Carry: w bits UAdd w (u, v)

4 Visualizing (Mathematical) Integer Addition Integer Addition – 4-bit integers u, v – Compute true sum Add 4 (u, v) – Values increase linearly with u and v – Forms planar surface Add 4 (u, v) u v

5 Visualizing Unsigned Addition Wraps Around – If true sum ≥ 2 w – At most once 0 2w2w 2 w+1 UAdd 4 (u, v) u v True Sum Modular Sum Overflow

6 Unsigned Binary Integers Given an n-bit number Range: 0 to +2 n – 1 Example 0000 0000 0000 0000 0000 0000 0000 1011 2 = 0 + … + 1×2 3 + 0×2 2 +1×2 1 +1×2 0 = 0 + … + 8 + 0 + 2 + 1 = 11 10 Using 32 bits 0 to +4,294,967,295

7 How can we change our representation to include signed numbers?

8 How can we represent a negative sign in a computer? Key point – we need to represent it in a way which makes arithmetic easy!

9 Naïve Solution: Signed Integers Given n bits, we have 2 n values we can represent. – Assign half to positive integers. – Assign other half to negative integers. Use most significant bit as negative sign. Number is negative is the most significant bit is a 1, positive otherwise.

10 Naïve Solution: Signed Integers Advantage: Signed integers don’t change. – 0001 is still 1. Disadvantage – 0000 is 0 – 1000 is also 0

11 BinaryUnsignedSigned Magnitude 000000 000111 001022 001133 010044 010155 011066 011177 1000-0 1001 1010-2 1011-3 1100-4 1101-5 1110-6 1111-7

12 Problem: How do we do arithmetic? 0010 +1101 1111 2 + -3 = -7 OOPS!

13 Want a method which makes arithmetic simple! Challenge: For any positive number X, find a representation for –X such that: X + (-X) = 0 Ignoring Carry Out 0101 +???? 0000

14 Want a method which makes arithmetic simple! Challenge: For any positive number X, find a representation for –X such that: X + (-X) = 0 Ignoring Carry Out 0101 +1011 0000

15 2’s-Complement This representation, the number we can add to X to get 0, is known as the two’s complement. Short-cut to find it: – Flip the bits, 0 -> 1, 1 -> 0. – Add one to the result

16 Binary PatternUnsigned Integer Flipped BitsFlipped Bits + 12’s- complement Integer 00000111100000 0001111101111 0010211011110-2 0011311001101-3 0100410111100-4 0101510101011-5 0110610011010-6 0111710001001-7 10008011110008

17 Advantages of 2’s complement Only one number for 0 Addition is still addition. Subtraction is just addition. No new hardware needed.

18 2s-Complement Signed Integers Given an n-bit number Range: –2 n – 1 to +2 n – 1 – 1 Example 1111 1111 1111 1111 1111 1111 1111 1100 2 = –1×2 31 + 1×2 30 + … + 1×2 2 +0×2 1 +0×2 0 = –2,147,483,648 + 2,147,483,644 = –4 10 Using 32 bits –2,147,483,648 to +2,147,483,647

19 2s-Complement Signed Integers Bit 31 is sign bit – 1 for negative numbers – 0 for non-negative numbers –(–2 n – 1 ) can’t be represented Non-negative numbers have the same unsigned and 2s-complement representation Some specific numbers – 0:0000 0000 … 0000 – –1:1111 1111 … 1111 – Most-negative:1000 0000 … 0000 – Most-positive:0111 1111 … 1111

20 Signed Negation Complement and add 1 – Complement means 1 → 0, 0 → 1 Example: negate +2 +2 = 0000 0000 … 0010 2 –2 = 1111 1111 … 1101 2 + 1 = 1111 1111 … 1110 2

21 Mapping Signed  Unsigned Signed 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 Unsigned 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

22 Unsigned & Signed Numeric Values Equivalence – Same encodings for nonnegative values Uniqueness – Every bit pattern represents unique integer value – Each representable integer has unique bit encoding  Can Invert Mappings – U2B(x) = B2U -1 (x) Bit pattern for unsigned integer – T2B(x) = B2T -1 (x) Bit pattern for two’s comp integer XB2T(X)B2U(X) 00000 00011 00102 00113 01004 01015 01106 01117 –88 –79 –610 –511 –412 –313 –214 –115 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7

23 T2U T2BB2U Two’s Complement Unsigned Maintain Same Bit Pattern xux X Mapping Between Signed & Unsigned U2T U2BB2T Two’s Complement Unsigned Maintain Same Bit Pattern uxx X Mappings between unsigned and two’s complement numbers: keep bit representations and reinterpret

24 Mapping Signed  Unsigned Signed 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 Unsigned 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 U2TT2U

25 Mapping Signed  Unsigned Signed 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 Unsigned 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 = +/- 16

26 0 TMax TMin –1 –2 0 UMax UMax – 1 TMax TMax + 1 2’s Complement Range Unsigned Range Conversion Visualized 2’s Comp.  Unsigned – Ordering Inversion – Negative  Big Positive

27 Sign Extension Task: – Given w-bit signed integer x – Convert it to w+k-bit integer with same value Rule: – Make k copies of sign bit: – X = x w–1,…, x w–1, x w–1, x w–2,…, x 0 k copies of MSB X X w w k

28 Sign Extension Example Converting from smaller to larger integer data type C automatically performs sign extension short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y;

29 Two’s Complement Addition TAdd and UAdd have Identical Bit-Level Behavior – Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v – Will give s == t u v + u + v True Sum: w+1 bits Operands: w bits Discard Carry: w bits TAdd w (u, v)

30 TAdd Overflow Functionality – True sum requires w+1 bits – Drop off MSB – Treat remaining bits as 2’s comp. integer –2 w –1 –1 –2 w 0 2 w –1 True Sum TAdd Result 1 000…0 1 011…1 0 000…0 0 100…0 0 111…1 100…0 000…0 011…1 PosOver NegOver

31 Visualizing 2’s Complement Addition Values – 4-bit two’s comp. – Range from -8 to +7 Wraps Around – If sum  2 w–1 Becomes negative At most once – If sum < –2 w–1 Becomes positive At most once TAdd 4 (u, v) u v PosOver NegOver

32 Characterizing TAdd Functionality – True sum requires w+1 bits – Drop off MSB – Treat remaining bits as 2’s comp. integer (NegOver) (PosOver) u v < 0> 0 < 0 > 0 Negative Overflow Positive Overflow TAdd(u, v) 2w2w 2w2w

33 Mathematical Properties of TAdd Isomorphic Group to unsigneds with UAdd – TAdd w (u, v) = U2T(UAdd w (T2U(u ), T2U(v))) Since both have identical bit patterns Two’s Complement Under TAdd Forms a Group – Closed, Commutative, Associative, 0 is additive identity – Every element has additive inverse

34 Representing Pointers Different compilers & machines assign different locations to objects int B = -15213; int *P = &B; int B = -15213; int *P = &B; x86-64SunIA32 EF FF FB 2C D4 F8 FF BF 0C 89 EC FF 7F 00

35 Strings How might we represent a string?

36 char S[6] = "18243"; Representing Strings Strings in C – Represented by array of characters – Each character encoded in ASCII format Standard 7-bit encoding of character set Character “0” has code 0x30 – Digit i has code 0x30+i – String should be null-terminated Final character = 0 Compatibility – Byte ordering not an issue Linux/AlphaSun 31 38 32 34 33 00 31 38 32 34 33 00


Download ppt "Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier."

Similar presentations


Ads by Google