Integers II, Floating Point I CSE 351, Summer 2019

Integers II, Floating Point I CSE 351, Summer 2019

L05: Integers II, Floating Point I CSE 351, Summer 2019 Integers II, Floating Point I CSE 351 Summer 2019 Instructor: Teaching Assistants: Sam Wolfson Rehaan Bhimani Corbin Modica Daniel Hsu http://xkcd.com/1953/ L05: Integers II, Floating Point I CSE 351, Summer 2019 Administrivia v Lab 1a due Friday (7/5) § Submit pointer.c and lab1Areflect.txt to Canvas v Lab 1b released soon, due Thursday 7/11 § Bit puzzles on number representation § Have much of what you need after today, will need floating point, coming soon § Section (on Friday) will be useful! § Bonus slides at the end of today’s lecture have relevant examples 2 L05: Integers II, Floating Point I CSE 351, Summer 2019 Extra Credit v All labs starting with Lab 1b have extra credit portions § These are meant to be fun extensions to the labs v Extra credit points don't affect your lab grades § They will be accumulated over the course and will be used to bump up borderline grades at the end of the quarter § Make sure you finish the rest of the lab before attempting any extra credit v Extra credit must be submitted separately on Gradescope § Additional “extra credit” assignment for each lab 3 L05: Integers II, Floating Point I CSE 351, Summer 2019 Integers v Binary representation of integers § Unsigned and signed § Casting in C v Consequences of finite width representations § Overflow, sign extension v Shifting and arithmetic operations 4 L05: Integers II, Floating Point I CSE 351, Summer 2019 Two’s Complement Recap v Accomplished with one neat mathematical trick! "#$ ( b"#$ has weight −2 , other bits have usual weights +2 bw-1 bw-2 . b0 § 4-bit Examples: – 1 + 0 • 10102 unsigned: – 2 1111 0000 + 1 3 2 1 0 1*2 +0*2 +1*2 +0*2 = 10 – 3 1110 0001 + 2 • 10102 two’s complement: 1101 0010 3 2 1 0 – 4 + 3 -1*2 +0*2 +1*2 +0*2 = –6 1100 0011 Two’s Complement § -1 represented as: – 5 1011 0100 + 4 3 3 1010 0101 11112 = -2 +(2 – 1) – 6 1001 0110 + 5 • MSB makes it super negative, add up 1000 0111 all the other bits to get back up to -1 – 7 + 6 – 8 + 7 5 L05: Integers II, Floating Point I CSE 351, Summer 2019 Two’s Complement Arithmetic v The same addition procedure works for both unsigned and two’s complement integers § Simplifies hardware: only one algorithm for addition § Algorithm: simple addition, discard the highest carry bit • Called modular addition: result is sum modulo 2/ v 4-bit Examples: 4 0100 -4 1100 4 0100 +3 +0011 +3 +0011 -3 +1101 =7 =-1 =1 6 L05: Integers II, Floating Point I CSE 351, Summer 2019 Why Does Two’s Complement Work? v For all representable positive integers �, we want: bit representation of –� + bit representation of –� 0 (ignoring the carry-out bit) § What are the 8-bit negative encodings for the following? 00000001 00000010 11000011 + ???????? + ???????? + ???????? 00000000 00000000 00000000 7 L05: Integers II, Floating Point I CSE 351, Summer 2019 Why Does Two’s Complement Work? v For all representable positive integers �, we want: bit representation of –� + bit representation of –� 0 (ignoring the carry-out bit) § What are the 8-bit negative encodings for the following? 00000001 00000010 11000011 + 11111111 + 11111110 + 00111101 100000000 100000000 100000000 These are the bitwise complement plus 1! -x == ~x + 1 8 L05: Integers II, Floating Point I CSE 351, Summer 2019 Signed/Unsigned Conversion Visualized v Two’s Complement → Unsigned UMax § Ordering Inversion UMax–1 § Negative → Big Positive TMax+1 Unsigned TMax TMax Range 2’s Complement 0 0 Range –1 –2 TMin 9 L05: Integers II, Floating Point I CSE 351, Summer 2019 Values To Remember v Unsigned Values v Two’s Complement Values § UMin = 0b00…0 § TMin = 0b10…0 = 0 = −2/#$ § UMax = 0b11…1 § TMax = 0b01…1 = 2/ − 1 = 2/#$ − 1 § −1 = 0b11…1 v 64-bit Examples: Decimal Hex UMax 18,446,744,073,709,551,615 FF FF FF FF FF FF FF FF TMax 9,223,372,036,854,775,807 7F FF FF FF FF FF FF FF TMin -9,223,372,036,854,775,808 80 00 00 00 00 00 00 00 -1 -1 FF FF FF FF FF FF FF FF 0 0 00 00 00 00 00 00 00 00 10 L05: Integers II, Floating Point I CSE 351, Summer 2019 In C: Signed vs. Unsigned v Casting § Bits are unchanged, just interpreted differently! • int tx, ty; • unsigned int ux, uy; § Explicit casting • tx = (int) ux; • uy = (unsigned int) ty; § Implicit casting can occur during assignments or function calls • tx = ux; • uy = ty; 11 L05: Integers II, Floating Point I CSE 351, Summer 2019 Casting Surprises !! v Integer literals (constants) § By default, integer constants are considered signed integers • Hex constants already have an explicit binary representation § Use “U” (or “u”) suffix to explicitly force unsigned • Examples: 0U, 4294967259u v Expression Evaluation § When you mixed unsigned and signed in a single expression, then signed values are implicitly cast to unsigned § Including comparison operators <, >, ==, <=, >= 12 L05: Integers II, Floating Point I CSE 351, Summer 2019 Casting Surprises !! v 32-bit examples: § TMin = -2,147,483,648, TMax = 2,147,483,647 Left Constant Order Right Constant Interpretation 0 0U 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 -1 0 1111 1111 1111 1111 1111 1111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 -1 0U 1111 1111 1111 1111 1111 1111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 2147483647 -2147483648 0111 1111 1111 1111 1111 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0000 2147483647U -2147483648 0111 1111 1111 1111 1111 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0000 -1 -2 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1110 (unsigned) -1 -2 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1110 2147483647 2147483648U 0111 1111 1111 1111 1111 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0000 2147483647 (int) 2147483648U 0111 1111 1111 1111 1111 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0000 13 L05: Integers II, Floating Point I CSE 351, Summer 2019 Integers v Binary representation of integers § Unsigned and signed § Casting in C v Consequences of finite width representations § Overflow, sign extension v Shifting and arithmetic operations 14 L05: Integers II, Floating Point I CSE 351, Summer 2019 Arithmetic Overflow Bits Unsigned Signed v When a calculation produces a result 0000 0 0 that can’t be represented in the 0001 1 1 0010 2 2 current encoding scheme 0011 3 3 § Integer range limited by fixed width 0100 4 4 § Can occur in both the positive and negative 0101 5 5 directions 0110 6 6 0111 7 7 v 1000 8 -8 C and java ignore overflow exceptions 1001 9 -7 § You end up with a bad value in your 1010 10 -6 program and no warning/indication… oops! 1011 11 -5 1100 12 -4 1101 13 -3 1110 14 -2 1111 15 -1 15 L05: Integers II, Floating Point I CSE 351, Summer 2019 Overflow: Unsigned 2 v Addition: drop carry bit (−2 ) 15 1111 15 0 + 2 + 0010 14 1111 0000 1 17 10001 13 1110 0001 2 1101 0010 12 3 1 1100 0011 2 Unsigned v Subtraction: borrow (+2 ) 11 1011 0100 4 1 10001 1010 0101 - 2 - 0010 10 1001 0110 5 9 1000 0111 6 -1 1111 8 7 15 ±22 because of modular arithmetic 16 L05: Integers II, Floating Point I CSE 351, Summer 2019 Overflow: Two’s Complement v Addition: (+) + (+) = (−) result? 6 0110 – 1 0 + 3 + 0011 – 2 1111 0000 + 1 9 1001 – 3 1110 0001 + 2 1101 0010 -7 – 4 + 3 1100 Two’s 0011 v Subtraction: (−) + (−) = (+)? Complement – 5 1011 0100 + 4 -7 1001 1010 0101 - 3 - 0011 – 6 1001 0110 + 5 – 7 1000 0111 + 6 -10 0110 – 8 + 7 6 For signed: overflow if operands have same sign and result’s sign is different 17 L05: Integers II, Floating Point I CSE 351, Summer 2019 Sign Extension v What happens if you convert a signed integral data type to a larger one? § e.g. char → short → int → long v 4-bit → 8-bit Example: § Positive Case 4-bit: 0010 = +2 ✓ • Add 0’s? 8-bit: ????00000010 = +2? § Negative Case? 18 L05: Integers II, Floating Point I CSE 351, Summer 2019 Sign Extension v Task: Given a �-bit signed integer X, convert it to �+�-bit signed integer X′ with the same value v Rule: Add � copies of sign bit § Let �8 be the �-th digit of X in binary : § X = �/#$, … , �/#$, �/#$, �/#>, … , �$, �? � copies of MSB original X � X • • • • • • Xʹ • • • • • • � � 20 L05: Integers II, Floating Point I CSE 351, Summer 2019 Sign Extension Example v Convert from smaller to larger integral data types v C automatically performs sign extension § Java too short int x = 12345; int ix = (int) x; short int y = -12345; int iy = (int) y; Var Decimal Hex Binary x 12345 30 39 00110000 00111001 ix 12345 00 00 30 39 00000000 00000000 00110000 00111001 y -12345 CF C7 11001111 11000111 iy -12345 FF FF CF C7 11111111 11111111 11001111 11000111 21 L05: Integers II, Floating Point I CSE 351, Summer 2019 Integers v Binary representation of integers § Unsigned and signed § Casting in C v Consequences of finite width representations § Overflow, sign extension v Shifting and arithmetic operations 22 L05: Integers II, Floating Point I CSE 351, Summer 2019 Shift Operations v Left shift (x<<n) bit vector x by n positions § Throw away (drop) extra bits on left § Fill with 0s on right v Right shift (x>>n) bit-vector x by n positions § Throw away (drop) extra bits on right § Logical shift (for unsigned values) • Fill with 0s on left § Arithmetic shift (for signed values) If the number is signed, • Replicate most significant bit on left extend the sign! • Maintains sign of x If it’s not, extend the naught.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    42 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us