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. 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

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 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 ±2 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 � 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 right § (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.

23 L05: Integers II, Floating Point I CSE 351, Summer 2019 Shift Operations x 0010 0010 x<<3 0001 0000 v Left shift (x<>2 0000 1000 § Fill with 0s on right arithmetic: x>>2 0000 1000 v Right shift (x>>n) § Logical shift (for unsigned values) x 1010 0010 • Fill with 0s on left x<<3 0001 0000 § Arithmetic shift (for signed values) logical: x>>2 0010 1000 • Replicate most significant bit on left arithmetic: x>>2 1110 1000 v Notes: § Shifts by n<0 or n≥w (w is bit width of x) are undefined § In C: behavior of >> is determined by compiler • In gcc / C lang, depends on data type of x (signed/unsigned) § In Java: logical shift is >>> and arithmetic shift is >> 24 L05: Integers II, Floating Point I CSE 351, Summer 2019 Shifting Arithmetic? v What are the following computing? § x>>n • 0b 0100 >> 1 = 0b 0010 • 0b 0100 >> 2 = 0b 0001 • Divide by 2n § x<

25 L05: Integers II, Floating Point I CSE 351, Summer 2019 Left Shifting Arithmetic 8-bit Example

v No difference in left shift operation for unsigned and signed numbers (just manipulates bits) § Difference comes during interpretation: x*2n? Signed Unsigned x = 25; 00011001 = 25 25

L1=x<<2; 0001100100 = 100 100

L2=x<<3; 00011001000 = -56 200 signed overflow L3=x<<4; 000110010000 = -112 144 unsigned overflow 26 L05: Integers II, Floating Point I CSE 351, Summer 2019 Right Shifting 8-bit Examples v Reminder: C operator >> does logical shift on unsigned values and arithmetic shift on signed values § Logical Shift: x/2n?

xu = 240u; 11110000 = 240

R1u=xu>>3; 00011110000 = 30

R2u=xu>>5; 0000011110000 = 7

rounding (down)

27 L05: Integers II, Floating Point I CSE 351, Summer 2019 Right Shifting Arithmetic 8-bit Examples v Reminder: C operator >> does logical shift on unsigned values and arithmetic shift on signed values § Arithmetic Shift: x/2n?

xs = -16; 11110000 = -16

R1s=xu>>3; 11111110000 = -2

R2s=xu>>5; 1111111110000 = -1

rounding (down)

28 L05: Integers II, Floating Point I CSE 351, Summer 2019 Summary v Sign and unsigned variables in C § Bit pattern remains the same, just interpreted differently § Strange things can happen with our arithmetic when we convert/cast between sign and unsigned numbers • Type of variables affects behavior of operators (shifting, comparison) v We can only represent so many numbers in � bits § When we exceed the limits, arithmetic overflow occurs § Sign extension tries to preserve value when expanding v Shifting is a useful bitwise operator § Right shifting can be arithmetic (sign) or logical (0) § Can be used in multiplication with constant or bit masking

29 L05: Integers II, Floating Point I CSE 351, Summer 2019 Number Representation Revisited v We know how to represent: § Signed and Unsigned Integers § Characters (ASCII) § Addresses v How do we encode the following: § Real numbers (e.g. 3.14159) § Very large numbers (e.g. 6.02×1023) Floating § Very small numbers (e.g. 6.626×10-34) Point § Special numbers (e.g. ∞, NaN)

30 L05: Integers II, Floating Point I CSE 351, Summer 2019 Floating Point Topics v Fractional binary numbers v IEEE floating-point standard v Floating-point operations and rounding v Floating-point in C

v There are many more details that we won’t cover § It’s a 58-page standard… 31 L05: Integers II, Floating Point I CSE 351, Summer 2019 Floating Point Summary v Floats also suffer from the fixed number of bits available to represent them § Can get overflow/underflow, just like ints § “Gaps” produced in representable numbers means we can lose precision, unlike ints • Some “simple fractions” have no exact representation (e.g. 0.2) • “Every operation gets a slightly wrong result” v Floating point arithmetic not associative or distributive § Mathematically equivalent ways of writing an expression may compute different results v Never test floating point values for equality! v Careful when converting between ints and floats! 32 L05: Integers II, Floating Point I CSE 351, Summer 2019 Representation of Fractions v “Binary Point,” like decimal point, signifies boundary between integer and fractional parts:

Example 6-bit xx.yyyy representation: 21 -4 20 2-1 2-2 2-3 2

1 -1 -3 v Example: 10.10102 = 1×2 + 1×2 + 1×2 = 2.62510

33 L05: Integers II, Floating Point I CSE 351, Summer 2019

Fractional Binary Numbers 2i 2i–1

4 • • • 2 1 bi bi–1 • • • b2 b1 b0 . b–1 b–2 b–3 • • • b–j

1/2 1/4 • • • 1/8

2–j

v Representation § Bits to right of “binary point” represent fractional powers of 2 i § Represents rational number: k å bk ×2 k=- j 34 L05: Integers II, Floating Point I CSE 351, Summer 2019 Fractional Binary Numbers v Value Representation

§ 5 and 3/4 101.112

§ 2 and 7/8 10.1112

§ 47/64 0.1011112 v Observations § Shift left = multiply by power of 2 § Shift right = divide by power of 2

§ Numbers of the form 0.111111…2 are just below 1.0 § 1/2 + 1/4 + 1/8 + … + 1/2i + … ➙ 1.0 § Use notation 1.0 – ε

35 L05: Integers II, Floating Point I CSE 351, Summer 2019 Limits of Representation v Limitations: § Even given an arbitrary number of bits, can only exactly represent numbers of the form x * 2y (y can be negative) § Other rational numbers have repeating bit representations

Value: Binary Representation:

• 1/3 = 0.333333…10 = 0.01010101[01]…2

• 1/5 = 0.001100110011[0011 ]…2

• 1/10 = 0.0001100110011[0011 ]…2

36 L05: Integers II, Floating Point I CSE 351, Summer 2019 Fixed Point Representation v Implied binary point. Two example schemes: #1: the binary point is between bits 2 and 3 b7 b6 b5 b4 b3 [.] b2 b1 b0 #2: the binary point is between bits 4 and 5 b7 b6 b5 [.] b4 b3 b2 b1 b0 v Wherever we put the binary point, with fixed point representations there is a trade off between the amount of range and precision we have v Fixed point = fixed range and fixed precision § range: difference between largest and smallest numbers possible § precision: smallest possible difference between any two numbers v Hard to pick how much you need of each! 37 L05: Integers II, Floating Point I CSE 351, Summer 2019 Floating Point Representation v Analogous to scientific notation § In Decimal: • Not 12000000, but 1.2 x 107 In C: 1.2e7 • Not 0.0000012, but 1.2 x 10-6 In C: 1.2e-6 § In Binary: • Not 11000.000, but 1.1 x 24 • Not 0.000101, but 1.01 x 2-4 v We have to divvy up the bits we have (e.g., 32) among: § the sign (1 bit) § the mantissa (significand) § the exponent

38 L05: Integers II, Floating Point I CSE 351, Summer 2019 Scientific Notation (Decimal)

mantissa exponent 23 6.0210 × 10 decimal point radix (base) v Normalized form: exactly one digit (non-zero) to left of decimal point v Alternatives to representing 1/1,000,000,000 § Normalized: 1.0×10-9 § Not normalized: 0.1×10-8,10.0×10-10

39 L05: Integers II, Floating Point I CSE 351, Summer 2019 Scientific Notation (Binary)

mantissa exponent -1 1.012 × 2 binary point radix (base) v Computer arithmetic that supports this called floating point due to the “floating” of the binary point § Declare such variable in C as float (or double)

40 L05: Integers II, Floating Point I CSE 351, Summer 2019 Scientific Notation Translation v Convert from scientific notation to binary point § Perform the multiplication by shifting the decimal until the exponent disappears 4 • Example: 1.0112×2 = 101102 = 2210 -2 • Example: 1.0112×2 = 0.010112 = 0.3437510 v Convert from binary point to normalized scientific notation § Distribute out exponents until binary point is to the right of a single digit 3 • Example: 1101.0012 = 1.1010012×2

v Practice: Convert 11.37510 to binary scientific notation

41 L05: Integers II, Floating Point I CSE 351, Summer 2019 Floating Point Topics v Fractional binary numbers v IEEE floating-point standard v Floating-point operations and rounding v Floating-point in C

v There are many more details that we won’t cover § It’s a 58-page standard… 42 L05: Integers II, Floating Point I CSE 351, Summer 2019 IEEE Floating Point v IEEE 754 § Established in 1985 as uniform standard for floating point arithmetic § Main idea: make numerically sensitive programs portable § Specifies two things: representation and result of floating operations § Now supported by all major CPUs v Driven by numerical concerns § Scientists/numerical analysts want them to be as real as possible § Engineers want them to be easy to implement and fast § In the end: • Scientists mostly won out • Nice standards for rounding, overflow, underflow, but... • Hard to make fast in hardware • Float operations can be an order of magnitude slower than integer ops

43