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