Arithmetic Topics Basic operations Programming Implications

Slides:



Advertisements
Similar presentations
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
Advertisements

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.
“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.
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)
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.
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.
1 Integer Operations. 2 Outline Arithmetic Operations –overflow –Unsigned addition, multiplication –Signed addition, negation, multiplication –Using Shift.
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.
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.
Integer Multiplication, Division Arithmetic shift Twice the number of places MIPS multiply unit. mult, multu Significant bits Mfhi, mflo, div, divu Arithmetic.
“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.
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.
Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier.
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.
David Kauchak CS 52 – Spring 2017
Integer Multiplication, Division Arithmetic shift
Instructors: Greg Kesden
Chapter 4 Operations on Bits.
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
Bits, Bytes, and Integers
University of Washington
Instructor: David Ferry
Section 2: Integer & Floating Point Numbers
Integer Representations and Arithmetic
Computer Systems Introduction
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”
Lecture 8: Addition, Multiplication & Division
C Puzzles Taken from old exams
CS140 Lecture 08: Data Representation: Bits and Ints
Comp Integers Spring 2015 Topics Numeric Encodings
Representing Information (2)
Computer Systems Introduction
Integers II CSE 410 Winter 2017 Instructor: Teaching Assistants:
Bits, Bytes, and Integers January 16, 2008
Integers Topics Numeric Encodings Programming Implications
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Bits, Bytes, and Integers
Representing Information (2)
Computer Organization COMP 210
Bits, Bytes, and Integers Part 2 3rd Lectures
Computer Organization COMP 210
Computer Organization COMP 210
Operations and Arithmetic
Bits, Bytes, and Integers
Computer Systems Introduction
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
Presentation transcript:

Arithmetic Topics Basic operations Programming Implications Addition, negation, multiplication Programming Implications Consequences of overflow Using shifts to perform power-of-2 multiply/divide

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

Visualizing Integer Addition 4-bit integers u, v Compute true sum Values increase linearly with u and v Forms planar surface But, our sum must fit in a 4-bit register... Add4(u , v) v u

Visualizing Unsigned Addition Wraps Around At most once if true sum ≥ 2w Overflow UAdd4(u , v) True Sum 2w 2w+1 Overflow v Modular Sum u

Two’s Complement Addition u • • • Operands: w bits + v • • • True Sum: w+1 bits u + v • • • 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

Visualizing 2’s Comp. 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 TAdd4(u , v) v u PosOver

Detecting 2’s Comp. Overflow Task Given s = TAddw(u , v) Determine if s = Addw(u , v) Claim Overflow iff either: u, v < 0, s  0 (NegOver) u, v  0, s < 0 (PosOver) Example int s, u, v; s = u + v; ovf = (u<0 == v<0) && (u<0 != s<0); 2w –1 2w–1 PosOver NegOver

Unsigned Multiplication in C • • • Operands: w bits * v • • • True Product: 2*w bits u · v • • • • • • UMultw(u , v) • • • Discard w bits: w bits Standard Multiplication Function Ignores high order w bits Implements Modular Arithmetic UMultw(u , v) = u · v mod 2w

Unsigned vs. Signed Multiplication Unsigned Multiplication unsigned ux, uy; unsigned up = ux * uy Truncates product to w-bit number up = UMultw(ux, uy) Modular arithmetic: up = ux  uy mod 2w Two’s Complement Multiplication int x, y; int p = x * y; Compute exact product of two w-bit numbers x, y Truncate result to w-bit number p = TMultw(x, y)

Unsigned vs. Signed Multiplication Unsigned Multiplication unsigned ux, uy; unsigned up = ux * uy Two’s Complement Multiplication int x, y; int p = x * y; Relation Signed multiplication gives same bit-level result as unsigned up == (unsigned) p

Power-of-2 Multiply with Shift Operation u << k gives u * 2k Both signed and unsigned Examples u << 3 == u * 8 (u << 3) + u == u * 9 Most machines shift and add much faster than multiply Compiler generates this code automatically k u • • • Operands: w bits * 2k ••• 1 ••• True Product: w+k bits u · 2k • • • ••• UMultw(u , 2k) ••• ••• Discard k bits: w bits TMultw(u , 2k)

Unsigned Power-of-2 Divide w/Shift Quotient of Unsigned by Power of 2 u >> k gives  u / 2k  Uses logical shift k u Binary Point ••• ••• Operands: / 2k ••• 1 ••• Division: u / 2k . ••• ••• ••• Result:  u / 2k  ••• •••

Arithmetic Surprises Assume 32-bit integers, signed x and y, unsigned ux Are the following expressions always true? x < 0  ((x*2) < 0) ux >= 0 x & 7 == 7  (x<<30) < 0 ux > -1 x > y  -x < -y x * x >= 0 x > 0 && y > 0  x + y > 0 x >= 0  -x <= 0 x <= 0  -x >= 0 x < 0  ((x*2) < 0) False: TMin ux >= 0 True: 0 = UMin x & 7 == 7  (x<<30) < 0 True: x2 = 1 ux > -1 False: 0 x > y  -x < -y False: -1, TMin x * x >= 0 False: 30426 x > 0 && y > 0  x + y > 0 False: TMax, TMax x >= 0  -x <= 0 True: –TMax < 0 x <= 0  -x >= 0 False: TMin