15-213 “The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules CS 213 F’00.

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.
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 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.
ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.
February 4, 2003 CSCE 212 Computer Architecture Lecture 3 Representing Integers.
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
Carnegie Mellon 1 This Week: Integers Integers  Representation: unsigned and signed  Conversion, casting  Expanding, truncating  Addition, negation,
Computer Arithmetic A Programmer’s View Oct. 6, 1998 Topics Integer Arithmetic –Unsigned –Two’s Complement Floating Point –IEEE Floating Point Standard.
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.
– 1 – CS 105 Computer Systems Introduction Topics: Class Intro Data Representation CS 105 “Tour of the Black Holes of Computing!” Geoff Kuenning Fall 2015.
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.
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
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.
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
University of Washington Announcements Lab 1, how is it going? Accounts all set? Coursecast Readings! Before class! 1.
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.
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.
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
University of Washington Roadmap car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Integers Topics Representations of Integers Basic properties and operations Implications for C.
Bits, Bytes, and Integers September 1, 2006 Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations.
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.
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
Integer Representations and Arithmetic
Computer Systems Introduction
“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”
CS140 Lecture 08: Data Representation: Bits and Ints
Comp Integers Spring 2015 Topics Numeric Encodings
Computer Systems Introduction
Integers II CSE 410 Winter 2017 Instructor: Teaching Assistants:
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.
Representing Information (2)
Integers II CSE 351 Autumn 2018 Instructor: Teaching Assistants:
Representing Information (2)
Bits, Bytes, and Integers
Computer Organization COMP 210
Integer Representations Jan. 23, 2001
Computer Organization COMP 210
Computer Organization COMP 210
Bits, Bytes, and Integers
Computer Systems Introduction
Integers II CSE 351 Spring 2019 Instructor: Teaching Assistants:
Presentation transcript:

“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules CS 213 F’00 class03.ppt Integer Representations Sep 5, 2000

CS 213 F’00– 2 – class03.ppt Notation W: Number of Bits in “Word” C Data TypeTypical 32-bitAlpha long int 3264 int 3232 short 1616 char 88 Integers Lower case E.g., x, y, z Bit Vectors Upper Case E.g., X, Y, Z Write individual bits as integers with value 0 or 1 E.g., X = x w–1, x w–2,… x 0 –Most significant bit on left

CS 213 F’00– 3 – class03.ppt Encoding Integers short int x = 15213; short int y = ; C short 2 bytes long Sign Bit For 2’s complement, most significant bit indicates sign 0 for nonnegative 1 for negative Unsigned Two’s Complement Sign Bit

CS 213 F’00– 4 – class03.ppt Encoding Example (Cont.) x = 15213: y = :

CS 213 F’00– 5 – class03.ppt Other Encoding Schemes Other less common encodings One’s complement: Invert bits for negative numbers Sign magnitude: Invert sign bit for negative numbers short int examples ISO C does not define what encoding machines use for signed integers, but 99% (or more) use two’s complement. For truly portable code, don’t count on it.

CS 213 F’00– 6 – class03.ppt Numeric Ranges Unsigned Values UMin=0 000…0 UMax = 2 w – 1 111…1 Two’s Complement Values TMin= –2 w–1 100…0 TMax = 2 w–1 – 1 011…1 Other Values Minus 1 111…1 Values for W = 16

CS 213 F’00– 7 – class03.ppt Values for Different Word Sizes Observations |TMin | = TMax + 1 –Asymmetric range UMax=2 * TMax + 1 C Programming #include –K&R, App. B11 Declares constants, e.g., – ULONG_MAX – LONG_MAX – LONG_MIN Values platform-specific

CS 213 F’00– 8 – class03.ppt Unsigned & Signed Numeric Values Example Values W = 4 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 –

CS 213 F’00– 9 – class03.ppt short int x = 15213; unsigned short int ux = (unsigned short) x; short int y = ; unsigned short int uy = (unsigned short) y; Casting Signed to Unsigned C Allows Conversions from Signed to Unsigned Resulting Value No change in bit representation Nonnegative values unchanged –ux = Negative values change into (large) positive values –uy = 50323

CS 213 F’00– 10 – class03.ppt T2U T2BB2U Two’s Complement Unsigned Maintain Same Bit Pattern xux X Relation Between 2’s Comp. & Unsigned ux x - w–10 +2 w–1 – –2 w–1 = 2*2 w–1 = 2 w

CS 213 F’00– 11 – class03.ppt Relation Between Signed & Unsigned uy = y + 2 * 32768=y

CS 213 F’00– 12 – class03.ppt From Two’s Complement to Unsigned T2U(x) = B2U(T2B(x)) = x + x w–1 2 w What you get in C: unsigned t2u(int x) { return (unsigned) x; } + 16 – XB2T(X)B2U(X) T2U(x) Identity

CS 213 F’00– 13 – class03.ppt From Unsigned to Two’s Complement U2T(x) = B2T(U2B(x)) = x – x w–1 2 w What you get in C: int u2t(unsigned x) { return (int) x; } – 16 – XB2T(X)B2U(X) U2T(x) Identity

CS 213 F’00– 14 – class03.ppt Signed vs. Unsigned in C Constants By default are considered to be signed integers Unsigned if have “U” as suffix 0U, U Casting Explicit casting between signed & unsigned same as U2T and T2U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; Implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty;

CS 213 F’00– 15 – class03.ppt Casting Surprises Expression Evaluation If mix unsigned and signed in single expression, signed values implicitly cast to unsigned Including comparison operations, ==, = Examples for W = 32 Constant 1 Constant 2 Relation Evaluation 00U== unsigned -10< signed -10U> unsigned > signed U < unsigned -1-2 > signed (unsigned) -1-2 > unsigned U < unsigned (int) U> signed

CS 213 F’00– 16 – class03.ppt 0 TMax TMin –1 –2 0 UMax UMax – 1 TMax TMax + 1 2’s Comp. Range Unsigned Range Explanation of Casting Surprises 2’s Comp.  Unsigned Ordering Inversion Negative  Big Positive

CS 213 F’00– 17 – class03.ppt 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

CS 213 F’00– 18 – class03.ppt 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;

CS 213 F’00– 19 – class03.ppt Justification For Sign Extension Prove Correctness by Induction on k Induction Step: extending by single bit maintains value Key observation: –2 w–1 = –2 w +2 w–1 Look at weight of upper bits: X –2 w–1 x w–1 X –2 w x w–1 + 2 w–1 x w–1 =–2 w–1 x w–1 - X X -+ w+1 w

CS 213 F’00– 20 – class03.ppt Casting Order Dependencies iux = 15213: iuy = 50323: uix = 15213: uiy = : uuy = : short int x = 15213; short int y = ; unsigned iux = (unsigned)(unsigned short) x; unsigned iuy = (unsigned)(unsigned short) y; unsigned uix = (unsigned) (int) x; unsigned uiy = (unsigned) (int) y; unsigned uuy = y; y unsignedFill 0’s unsignedSign Extend iuy uiy (unsigned short)(unsigned) (int)(unsigned)

CS 213 F’00– 21 – class03.ppt Why Should I Use Unsigned? Don’t Use Just Because Number Nonzero C compiler on Alpha generates less efficient code –Comparable code on Intel/Linux unsigned i; for (i = 1; i < cnt; i++) a[i] += a[i-1]; Easy to make mistakes for (i = cnt-2; i >= 0; i--) a[i] += a[i+1]; Do Use When Performing Modular Arithmetic Multiprecision arithmetic Other esoteric stuff Do Use When Need Extra Bit’s Worth of Range Working right up to limit of word size

CS 213 F’00– 22 – class03.ppt Negating with Complement & Increment In C ~x + 1 == -x Complement Observation: ~x + x == 1111…11 2 == -1 Increment ~x + x + (-x + 1)==-1 + (-x + 1) ~x + 1==-x Warning: Be cautious treating int ’s as integers OK here x ~x

CS 213 F’00– 23 – class03.ppt Comp. & Incr. Examples x =