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

Slides:



Advertisements
Similar presentations
HEXADECIMAL NUMBERS Code
Advertisements

When NOT to use Unsigned? Don’t Use Just Because Number Nonzero – C compilers on some machines generate less efficient code unsigned i; for (i = 1; i
Fabián E. Bustamante, Spring 2007 Integers Today Numeric Encodings Programming Implications Basic operations Programming Implications Next time Floats.
CS213 Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication.
Bits, Bytes, and Integers August 29, 2007 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules CS 213 F’00.
CS 151 Digital Systems Design Lecture 3 More Number Systems.
“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations.
CS213 Integers Apr 3, 2006 Topics Numeric Encodings
“The course that gives CMU its Zip!” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
February 4, 2003 CSCE 212 Computer Architecture Lecture 3 Representing Integers.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Carnegie Mellon 1 This Week: Integers Integers  Representation: unsigned and signed  Conversion, casting  Expanding, truncating  Addition, negation,
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
Int’s and Integers CENG331: Introduction to Computer Systems 3 rd Lecture Instructor: Erol Sahin Acknowledgement: Most of the slides are adapted from the.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bits, Bytes, and Integers MCS-284 : Computer Organization.
Carnegie Mellon 1 Bits, Bytes, and Integers Lecture, Jan. 24, 2013 These slides are from website which accompanies the book “Computer.
1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.
Comp Integers Spring 2015 Topics Numeric Encodings
– 1 – CS 105 Computer Systems Introduction Topics: Staff, text, and policies Lecture topics and assignments Lab rationale CS 105 “Tour of the Black Holes.
Lecture 3: Computation and Representation CS 2011 Fall 2014, Dr. Rozier.
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Carnegie Mellon 1 Bits, Bytes, and Integers : Introduction to Computer Systems 3 rd Lectures, May 27th, 2014 Instructors: Greg Kesden.
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
University of Washington Integers The Hardware/Software Interface CSE351 Winter 2013.
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Carnegie Mellon Introduction to Computer Systems /18-243, spring rd Lecture, Jan. 20 th Instructors: Gregory Kesden and Markus Püschel.
Carnegie Mellon 1 This week: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers  Representation: unsigned and.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Int’s and Integers CENG331: Introduction to Computer Systems 3 rd Lecture Instructor: Erol Sahin Acknowledgement: Most of the slides are adapted from the.
Carnegie Mellon 1 Bits, Bytes, and Integers : Introduction to Computer Systems 2 nd Lecture, Aug. 26, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Integers Topics Representations of Integers Basic properties and operations Implications for C.
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN Lecture 8 Dr. Shi Dept. of Electrical and Computer Engineering.
Bits, Bytes, and Integers September 1, 2006 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
Lecture 2 Two’s Complement Proofs Topics Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
Lecture 2 Ints revisited Topics Unsigned Integers Signed magnitude Two’s complement January 11, 2016 CSCE 212H Computer Architecture.
Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations –Addition, negation, multiplication.
– 1 – CS 105 Computer Systems Introduction Topics: Class Intro Data Representation CS 105 “Tour of the Black Holes of Computing!” Geoff Kuenning Fall 2014.
Instructors: Greg Kesden
Today’s Instructor: Randy Bryant
This Week: Integers Integers Summary
Integers Topics Numeric Encodings (2.2) Programming Implications
CS 367 Integers Topics (Ch. 2.2, 2.3) Numeric Encodings
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
University of Washington
Instructor: David Ferry
Section 2: Integer & Floating Point Numbers
Integer Representations and Arithmetic
“The course that gives CMU its Zip!”
CS213 Integers Topics Numeric Encodings Programming Implications
CS 105 “Tour of the Black Holes of Computing”
CS 105 “Tour of the Black Holes of Computing”
Representing Information (2)
Bits, Bytes, and Integers January 16, 2008
Bits, Bytes, and Integers 2nd Lectures
Integers Topics Numeric Encodings Programming Implications
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Arithmetic Topics Basic operations Programming Implications
Representing Information (2)
Computer Organization COMP 210
Bits, Bytes, and Integers Part 2 3rd Lectures
Integer Representations Jan. 23, 2001
Computer Organization COMP 210
Presentation transcript:

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