Integers Topics Numeric Encodings (2.2) Programming Implications

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Integers Today Numeric Encodings Programming Implications Basic operations Programming Implications Next time Floats.
Advertisements

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 CS 213 F’00.
“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.
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.
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.
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.
Bits, Bytes, and Integers CSCE 312
Today’s Instructor: Randy Bryant
This Week: Integers Integers Summary
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
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)
Arithmetic Topics Basic operations Programming Implications
Bits, Bytes, and Integers
Integers II CSE 351 Summer 2018 Instructor: Justin Hsia
Comp Org & Assembly Lang
Computer Organization COMP 210
Integer Representations Jan. 23, 2001
Computer Organization COMP 210
Computer Organization COMP 210
Bits, Bytes, and Integers
Computer Systems Introduction
Lecture 2: Bits, Bytes, Ints
Integers II CSE 351 Spring 2019 Instructor: Teaching Assistants:
Presentation transcript:

Integers Topics Numeric Encodings (2.2) Programming Implications Unsigned & Two’s complement Programming Implications C promotion rules Basic operations (2.3) Addition, negation, multiplication Consequences of overflow Using shifts to perform power-of-2 multiply/divide

Encoding Integers Sign Bit Unsigned Two’s Complement Sign Bit short int x = 15213; short int y = -15213; Sign Bit C short 2 bytes long Sign Bit For 2’s complement, most significant bit indicates sign 0 for nonnegative 1 for negative

Numeric Ranges Unsigned Values UMin = 0 UMax = 2w – 1 000…0 UMax = 2w – 1 111…1 Two’s Complement Values TMin = –2w–1 100…0 TMax = 2w–1 – 1 011…1 Other Values Minus 1 111…1 Values for W = 16

Unsigned & Signed Numeric Values Given n bits, all a matter of mapping (rule) X B2U(X) B2S(X) B2O(X) B2T(X) 000 001 1 010 2 011 3 100 4 -0 -3 -4 101 5 -1 -2 110 6 111 7

Unsigned & Signed Numeric Values X B2T(X) B2U(X) 0000 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 –8 8 –7 9 –6 10 –5 11 –4 12 –3 13 –2 14 –1 15 1000 1001 1010 1011 1100 1101 1110 1111 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

Values for Different Word Sizes C Programming  #include <limits.h> K&R App. B11 Declares constants, e.g.,  ULONG_MAX  LONG_MAX  LONG_MIN Values platform-specific Observations |TMin | = TMax + 1 Asymmetric range Umax = 2 * TMax + 1 -Tmin ?

Prob. 2.19 X T2B4(X) -8 -3 -2 -1 5

Decimal Numbers Digits: 0,1,…9 49 – 2 49 + (100 – 2) = 49 + 98 = 147 = 47 2 – 49 2 + (100 – 49) = 2 + 51 = 53 = -47 Pat. 10’s comp. 0 0 1 1 2 2 3 3 4 4 5 -5 6 -4 7 -3 8 -2 9 -1 Pat. 10’s comp 00 0 01 1 .. 49 49 50 -50 51 -49 52 -48 98 -2 99 -1

2.2. Casting Signed to Unsigned C Allows Conversions from Signed to Unsigned Resulting Value No change in bit representation Nonnegative values unchanged ux = 15213 Negative values change into (large) positive values uy = 50323 short int x = 15213; unsigned short int ux = (unsigned short) x; short int y = -15213; unsigned short int uy = (unsigned short) y;

Signed vs. Unsigned in C Constants Casting By default are considered to be signed integers Unsigned if have “U” as suffix 0U, 4294967259U 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;

Casting Surprises Expression Evaluation 0 == 0U Constant1 Constant2 If mix unsigned and signed in single expression, signed values implicitly cast to unsigned Including comparison operations <, >, ==, <=, >= Examples for W = 8 Constant1 Constant2 0 == 0U -1 < 0 -1 > 0U 127 > -128 127U < -128 (unsigned) -1 > -2 127 < 128U 127 > (int) 128U

Prob. 2.44 int x = foo(); /* arbitrary value */ int y = bar(); /* arbitrary value */ unsigned us = x; unsigned uy = y; For any x and y, are the following always true ? (x>0) || (x-1<0) (x & 7) !=7 || (x<<29 < 0) (x*x) >=0 x < 0 || -x <= 0 x > 0 || -x >= 0 x+y == uy+ux x * ~y + uy * ux == -x

Why Casting Surprises ? 2’s Comp.  Unsigned Ordering Inversion Negative  Big Positive TMax TMin –1 –2 UMax UMax – 1 TMax + 1 2’s Comp. Range Unsigned

Sign Extension Task: Rule: 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 • • • X X  w k k copies of MSB

Sign Extension Example short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y; Decimal Hex Binary x 15213 3B 6D 00111011 01101101 ix 00 00 3B 6D 00000000 00000000 00111011 01101101 y -15213 C4 93 11000100 10010011 iy FF FF C4 93 11111111 11111111 11000100 10010011 Converting from smaller to larger integer data type C automatically performs sign extension

Justification For Sign Extension Prove Correctness by Induction on k Induction Step: extending by single bit maintains value Key observation: –2w–1 = –2w +2w–1 Look at weight of upper bits: X –2w–1 xw–1 X  –2w xw–1 + 2w–1 xw–1 = –2w–1 xw–1 - • • • X X  + w+1 w

Shift Operations Left Shift: x << y Right Shift: x >> y Shift bit-vector x left y positions Throw away extra bits on left Fill with 0’s on right Right Shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with 0’s on left Arithmetic shift Replicate most significant bit on right Useful with two’s complement integer representation Argument x 01100010 << 3 00010000 00010000 00010000 Log. >> 2 00011000 0001100 00011000 Arith. >>2 00011000 00011000 0001100 Argument x 10100010 << 3 00010000 00010000 00010000 Log. >> 2 0010100 00101000 00101000 Arith. >> 2 11101000 11101000 1110100

Prob. 2.23 Given a 32-bit machine, int func1(unsigned word){ return (int) ((word <<24) >> 24); } int func2(unsigned word){ return ((int) word <<24) >> 24; w func1(w) func2(w) 0x00000076 0x87654321 0x000000C9 0xEDCBA987