Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier
Recall from Last Time Unsigned integer representation and mathematics.
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)
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
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
Unsigned Binary Integers Given an n-bit number Range: 0 to +2 n – 1 Example = 0 + … + 1× ×2 2 +1×2 1 +1×2 0 = 0 + … = 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 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.
Naïve Solution: Signed Integers Advantage: Signed integers don’t change. – 0001 is still 1. Disadvantage – 0000 is 0 – 1000 is also 0
BinaryUnsignedSigned Magnitude
Problem: How do we do arithmetic? = -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 Ignoring Carry Out ???? 0000
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
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 PatternUnsigned Integer Flipped BitsFlipped Bits + 12’s- complement Integer
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: –2 n – 1 to +2 n – 1 – 1 Example = –1× × … + 1×2 2 +0×2 1 +0×2 0 = –2,147,483, ,147,483,644 = –4 10 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 –(–2 n – 1 ) can’t be represented Non-negative numbers have the same unsigned and 2s-complement representation Some specific numbers – 0: … 0000 – –1: … 1111 – Most-negative: … 0000 – Most-positive: … 1111
Signed Negation Complement and add 1 – Complement means 1 → 0, 0 → 1 Example: negate = … –2 = … = …
Mapping Signed Unsigned Signed Unsigned Bits
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) –88 –79 –610 –511 –412 –313 –214 –
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
Mapping Signed Unsigned Signed Unsigned Bits U2TT2U
Mapping Signed Unsigned Signed Unsigned Bits = +/- 16
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
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
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 = ; int iy = (int) y;
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)
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… … … … …1 100…0 000…0 011…1 PosOver NegOver
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
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
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
Representing Pointers Different compilers & machines assign different locations to objects int B = ; int *P = &B; int B = ; int *P = &B; x86-64SunIA32 EF FF FB 2C D4 F8 FF BF 0C 89 EC FF 7F 00
Strings How might we represent a string?
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