Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier Recall from Last Time • Unsigned integer representation and mathematics. Unsigned Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u ••• + v ••• u+v ••• UAddw(u , v) ••• • Standard Addition Function – Ignores carry output • Implements Modular Arithmetic s = UAddw(u , v) = u + v mod 2w u v UAdd w (u,v) w u v 2 u v 2w u v 2w Visualizing (Mathematical) Integer Addition • Integer Addition –4-bit integers u, v –Compute true sum Add4(u , v) –Values increase linearly with u and v –Forms planar surface Add4(u , v) Integer Addition 32 28 24 20 16 14 12 12 10 8 8 4 6 0 4 0 2 u 4 2 6 8 10 12 0 14 v Visualizing Unsigned Addition • Wraps Around Overflow – If true sum ≥ 2w – At most once True Sum 2w+1 UAdd4(u , v) 16 14 Overflow 12 10 8 2w 14 6 12 10 4 8 2 0 6 0 Modular Sum 4 0 u 2 4 2 6 8 10 12 0 14 v Unsigned Binary Integers • Given an n-bit number Range: 0 to +2n – 1 Example 0000 0000 0000 0000 0000 0000 0000 10112 = 0 + … + 1×23 + 0×22 +1×21 +1×20 = 0 + … + 8 + 0 + 2 + 1 = 1110 Using 32 bits 0 to +4,294,967,295 How can we change our representation to include signed numbers? How can we represent a negative sign in a computer? • Key point – we need to represent it in a way which makes arithmetic easy! Naïve Solution: Signed Integers • Given n bits, we have 2n 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. Naïve Solution: Signed Integers • Advantage: Signed integers don’t change. – 0001 is still 1. • Disadvantage – 0000 is 0 – 1000 is also 0 Binary Unsigned Signed Magnitude 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 -0 -0 1001 -1 -1 1010 -2 -2 1011 -3 -3 1100 -4 -4 1101 -5 -5 1110 -6 -6 1111 -7 -7 Problem: How do we do arithmetic? + 0 0 1 0 1 1 0 1 1 1 1 1 2 + -3 = -7 OOPS! Want a method which makes arithmetic simple! • Challenge: For any positive number X, find a representation for –X such that: X + (-X) = 0 + 0 1 0 1 ? ? ? ? 0 0 0 0 Ignoring Carry Out Want a method which makes arithmetic simple! • Challenge: For any positive number X, find a representation for –X such that: X + (-X) = 0 + 0 1 0 1 1 0 1 1 0 0 0 0 Ignoring Carry Out 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 Binary Pattern Unsigned Integer Flipped Bits Flipped Bits + 1 2’scomplement Integer 0000 0 1111 0000 0 0001 1 1110 1111 -1 0010 2 1101 1110 -2 0011 3 1100 1101 -3 0100 4 1011 1100 -4 0101 5 1010 1011 -5 0110 6 1001 1010 -6 0111 7 1000 1001 -7 1000 8 0111 1000 8 Advantages of 2’s complement • Only one number for 0 • Addition is still addition. • Subtraction is just addition. • No new hardware needed. 2s-Complement Signed Integers • Given an n-bit number Range: –2n – 1 to +2n – 1 – 1 Example 1111 1111 1111 1111 1111 1111 1111 11002 = –1×231 + 1×230 + … + 1×22 +0×21 +0×20 = –2,147,483,648 + 2,147,483,644 = –410 Using 32 bits –2,147,483,648 to +2,147,483,647 2s-Complement Signed Integers • Bit 31 is sign bit – 1 for negative numbers – 0 for non-negative numbers • –(–2n – 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 Signed Negation • Complement and add 1 – Complement means 1 → 0, 0 → 1 x x 1111...1112 1 x 1 x Example: negate +2 +2 = 0000 0000 … 00102 –2 = 1111 1111 … 11012 + 1 = 1111 1111 … 11102 Mapping Signed Unsigned Bits Signed Unsigned 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 -8 8 1001 -7 9 1010 -6 10 1011 -5 11 1100 -4 12 1101 -3 13 1110 -2 14 1111 -1 15 Unsigned & Signed Numeric Values X 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 B2U(X) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B2T(X) 0 1 2 3 4 5 6 7 –8 –7 –6 –5 –4 –3 –2 –1 • 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 Mapping Between Signed & Unsigned Unsigned Two’s Complement T2U x T2B X B2U ux Maintain Same Bit Pattern Unsigned ux Two’s Complement U2T U2B X B2T x Maintain Same Bit Pattern • Mappings between unsigned and two’s complement numbers: keep bit representations and reinterpret Mapping Signed Unsigned Bits Signed Unsigned 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 0110 6 0111 7 1000 -8 8 1001 -7 9 1010 -6 10 1011 -5 11 1100 -4 12 1101 -3 13 1110 -2 14 1111 -1 15 T2U U2T 5 6 7 Mapping Signed Unsigned Bits Signed Unsigned 0000 0 0 0001 1 1 0010 2 2 0011 3 0100 4 0101 5 5 0110 6 6 0111 7 7 1000 -8 8 1001 -7 9 1010 -6 10 1011 -5 1100 -4 12 1101 -3 13 1110 -2 14 1111 -1 15 = +/- 16 3 4 11 Conversion Visualized • 2’s Comp. Unsigned UMax UMax – 1 – Ordering Inversion – Negative Big TMax Positive 2’s Complement Range 0 –1 –2 TMin TMax + 1 TMax 0 Unsigned Range 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 = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0 w k copies of MSB X ••• ••• X ••• k ••• w Sign Extension Example short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y; x ix y iy Decimal 15213 15213 -15213 -15213 Hex 3B 00 00 3B C4 FF FF C4 6D 6D 93 93 Binary 00111011 00000000 00000000 00111011 11000100 11111111 11111111 11000100 01101101 01101101 10010011 10010011 • Converting from smaller to larger integer data type • C automatically performs sign extension Two’s Complement Addition u ••• v ••• u+v ••• Operands: w bits + True Sum: w+1 bits Discard Carry: w bits TAddw(u , v) ••• • 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 TAdd Overflow • Functionality – True sum requires w+1 bits – Drop off MSB – Treat remaining bits as 2’s comp. integer True Sum 0 111…1 2w–1 PosOver TAdd Result 0 100…0 2w –1 011…1 0 000…0 0 000…0 1 011…1 –2w –1–1 100…0 1 000…0 –2w NegOver Visualizing 2’s Complement Addition NegOver • Values – 4-bit two’s comp. – Range from -8 to +7 • Wraps Around – If sum 2w–1 • Becomes negative • At most once – If sum < –2w–1 • Becomes positive • At most once TAdd4(u , v) 8 6 4 2 0 6 -2 4 2 -4 0 -6 -2 -8 -4 -8 -6 -4 u v -6 -2 0 2 4 -8 6 PosOver Characterizing TAdd • Functionality – True sum requires w+1 bits – Drop off MSB – Treat remaining bits as 2’s comp. integer Positive Overflow TAdd(u , v) >0 v <0 <0 u >0 Negative Overflow u v 22ww1 u v TMin w (NegOver) TAdd w (u,v) u v TMinw u v TMax w u v 22ww1 TMax w u v (PosOver) Mathematical Properties of TAdd • Isomorphic Group to unsigneds with UAdd – TAddw(u , v) = U2T(UAddw(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 u TCompw (u) TMinw u TMinw u TMinw Representing Pointers int B = -15213; int *P = &B; Sun IA32 x86-64 EF D4 0C FF F8 89 FB FF EC 2C BF FF FF 7F 00 00 Different compilers & machines assign different locations to objects Strings How might we represent a string? Representing Strings char S[6] = "18243"; • Strings in C – Represented by array of characters Linux/Alpha – Each character encoded in ASCII format 31 • Standard 7-bit encoding of character 38 set 32 • Character “0” has code 0x30 34 – Digit i has code 0x30+i 33 – String should be null-terminated 00 • Final character = 0 • Compatibility – Byte ordering not an issue Sun 31 38 32 34 33 00