Integers II, Floating Point I - University of Washington
Transcript of Integers II, Floating Point I - University of Washington
CSE351, Summer 2020L05: Integers II, Floating Point I
IntegersII,FloatingPointICSE351Summer2020
Instructor: TeachingAssistants:PorterJones AmyXu
CallumWalkerSamWolfsonTimMandzyuk
http://xkcd.com/1953/
CSE351, Summer 2020L05: Integers II, Floating Point I
Administrivia
v Questionsdoc:https://tinyurl.com/CSE351-7-1v hw4andhw5dueMonday7/6– 10:30amv hw6andhw7dueFriday7/10– 10:30am
§ WillpostMonday’sslideslatertodaysoyoucangetstarted
v Lab1adueMonday(7/6)(trytofinishbyFriday!)§ Submitpointer.c andlab1Areflect.txt toGradescope
v Lab1breleasedtomorrow,due7/10§ Bitmanipulationproblemsusingcustomdatatype§ Today’sbonusslideshavehelpfulexamples,omorrow’ssectionwillhavehelpfulexamplestoo
2
CSE351, Summer 2020L05: Integers II, Floating Point I
Gradescope LabTurnin
v MakesureyoupasstheFileandCompilationCheck!
v Doesn’tindicateifyoupassedalltests,justindicatesthatallthecorrectfileswerefoundandtherewerenocompilationorruntimeerrors.
v Usethetestingprogramsweprovidetocheckyoursolutionforcorrectness(onattu ortheVM)
3
CSE351, Summer 2020L05: Integers II, Floating Point I
QuickAside:CMacros
v Lab1bwillhaveyouusesomeCmacrosforbitmasksv Syntaxisoftheform:
#define NAME expressionv Cannowuse“NAME”insteadof“expression”incodev Usefultohelpwithreadability/factoringincode
§ Especiallyusefulfordefiningconstantssuchasbitmasks!
v AreNOTexactlythesameasaconstantinJava§ Doesnaïvecopyandreplacebefore compilation.§ Everywherethecharacters“NAME”appearinthecode,thecharacters“expression”willnowappearinstead.
v SeeLecture4(IntegersI)slidesforexampleusages4
CSE351, Summer 2020L05: Integers II, Floating Point I
Integers
v Binaryrepresentationofintegers§ Unsignedandsigned
v Shiftingandarithmeticoperations– usefulforLab1av InC:Signed,UnsignedandCastingv Consequencesoffinitewidthrepresentations
§ Overflow,signextension
5
CSE351, Summer 2020L05: Integers II, Floating Point I
Two’sComplementArithmetic
v Thesameadditionprocedureworksforbothunsignedandtwo’scomplementintegers§ Simplifieshardware: onlyonealgorithmforaddition§ Algorithm: simpleaddition,discardthehighestcarrybit
• Calledmodularaddition:resultissummodulo 2!
v 4-bitExamples:
6
HW. TC.
0100+0011
4+3
= 0111 = 7
HW. TC. HW. TC.
1100+0011
-4+3
0100+1101
4-3
= 1111 =-1 =10001 = 1
CSE351, Summer 2020L05: Integers II, Floating Point I
WhyDoesTwo’sComplementWork?
v Forallrepresentablepositiveintegers𝑥,wewant:
§ Whatarethe8-bitnegativeencodingsforthefollowing?
7
bitrepresentationof –𝑥+ bitrepresentationof –𝑥
0 (ignoringthecarry-outbit)
00000001+ ????????00000000
00000010+ ????????00000000
11000011+ ????????00000000
CSE351, Summer 2020L05: Integers II, Floating Point I
WhyDoesTwo’sComplementWork?
v Forallrepresentablepositiveintegers𝑥,wewant:
§ Whatarethe8-bitnegativeencodingsforthefollowing?
8
bitrepresentationof –𝑥+ bitrepresentationof –𝑥
0 (ignoringthecarry-outbit)
00000001+ 11111111100000000
00000010+ 11111110100000000
11000011+ 00111101100000000
Thesearethebitwisecomplementplus1!-x == ~x + 1
CSE351, Summer 2020L05: Integers II, Floating Point I
UMax – 1
0
TMax
TMin
–1–2
0/UMin
UMax
TMaxTMax +1
2’sComplementRange
UnsignedRange
Signed/UnsignedConversionVisualizedv Two’sComplement→ Unsigned
§ OrderingInversion§ Negative→ BigPositive
9
CSE351, Summer 2020L05: Integers II, Floating Point I
ValuesToRememberv UnsignedValues
§ UMin = 0b00…0= 0
§ UMax = 0b11…1= 2! − 1
v Example: Valuesfor𝑤 = 64
10
v Two’sComplementValues§ TMin = 0b10…0
= −2!"#
§ TMax = 0b01…1= 2!"# − 1
§ −1 = 0b11…1
Decimal HexUMax 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
CSE351, Summer 2020L05: Integers II, Floating Point I
Integers
v Binaryrepresentationofintegers§ Unsignedandsigned
v Shiftingandarithmeticoperations– usefulforLab1av InC:Signed,UnsignedandCastingv Consequencesoffinitewidthrepresentations
§ Overflow,signextension
11
CSE351, Summer 2020L05: Integers II, Floating Point I
InC:Signedvs.Unsigned
v Casting§ Bitsareunchanged,justinterpreteddifferently!
• int tx,ty;• unsigned int ux,uy;
§ Explicit casting• tx =(int)ux;• uy =(unsigned int)ty;
§ Implicit castingcanoccurduringassignmentsorfunctioncalls• tx =ux;• uy =ty;
12
CSE351, Summer 2020L05: Integers II, Floating Point I
CastingSurprises
v Integerliterals(constants)§ Bydefault,integerconstantsareconsideredsigned integers
• Hexconstantsalreadyhaveanexplicitbinaryrepresentation
§ Use“U”(or“u”)suffixtoexplicitlyforceunsigned• Examples: 0U,4294967259u
v ExpressionEvaluation§ Whenyoumixedunsignedandsignedinasingleexpression,thensignedvaluesareimplicitlycasttounsigned
§ Includingcomparisonoperators<,>,==,<=,>=
13
!!!
CSE351, Summer 2020L05: Integers II, Floating Point I
CastingSurprises
v 32-bitexamples:§ TMin =-2,147,483,648,TMax =2,147,483,647
14
!!!
LeftConstant Order RightConstant Interpretation000000000000000000000000000000000
0U00000000000000000000000000000000
-111111111111111111111111111111111
000000000000000000000000000000000
-111111111111111111111111111111111
0U00000000000000000000000000000000
214748364701111111111111111111111111111111
-214748364810000000000000000000000000000000
2147483647U01111111111111111111111111111111
-214748364810000000000000000000000000000000
-111111111111111111111111111111111
-211111111111111111111111111111110
(unsigned)-111111111111111111111111111111111
-211111111111111111111111111111110
214748364701111111111111111111111111111111
2147483648U10000000000000000000000000000000
214748364701111111111111111111111111111111
(int)2147483648U10000000000000000000000000000000
CSE351, Summer 2020L05: Integers II, Floating Point I
Integers
v Binaryrepresentationofintegers§ Unsignedandsigned
v Shiftingandarithmeticoperations– usefulforLab1av InC:Signed,UnsignedandCastingv Consequencesoffinitewidthrepresentations§ Overflow,signextension
15
CSE351, Summer 2020L05: Integers II, Floating Point I
ArithmeticOverflowv Whenacalculationproducesaresult
thatcan’tberepresentedinthecurrentencodingscheme§ Integerrangelimitedbyfixedwidth§ Canoccurinboththepositiveandnegative
directions
v CandJavaignoreoverflowexceptions§ Youendupwithabadvalueinyour
programandnowarning/indication…oops!
16
Bits Unsigned Signed0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 -81001 9 -71010 10 -61011 11 -51100 12 -41101 13 -31110 14 -21111 15 -1
CSE351, Summer 2020L05: Integers II, Floating Point I
Overflow:Unsigned
v Addition: dropcarrybit(−2!)
v Subtraction: borrow(+2!)
17
15+ 2171
1111+ 001010001
00000001
0011
11111110
1100
10111010
1000 01110110
0100
0010
01011001
1101
01
2
3
4
5
678
9
10
11
12
13
1415
Unsigned
1- 2-115
10001- 00101111
±2! becauseofmodulararithmetic
CSE351, Summer 2020L05: Integers II, Floating Point I
Overflow:Two’sComplement
v Addition: (+)+(+)=(−)result?
v Subtraction: (−)+(−)=(+)?
18
00000001
0011
11111110
1100
10111010
1000 01110110
0100
0010
01011001
1101
0+ 1
+ 2
+ 3
+ 4
+ 5
+ 6+ 7– 8
– 7
– 6
– 5
– 4
– 3
– 2– 1
Forsigned: overflowifoperandshavesamesignandresult’ssignisdifferent
Two’sComplement
6+ 39-7
0110+ 00111001
-7- 3-106
1001- 00110110
CSE351, Summer 2020L05: Integers II, Floating Point I
SignExtension
v Whathappensifyouconvertasigned integraldatatypetoalargerone?§ e.g. char→ short→ int→ long
v 4-bit→ 8-bitExample:§ PositiveCase
• Add0’s?
§ NegativeCase?
19
4-bit: 0010 = +28-bit: ????0010 = ?00000010 +2✓
CSE351, Summer 2020L05: Integers II, Floating Point I
PollingQuestion[Int II- a]
v Whichofthefollowing8-bitnumbershasthesamesigned valueasthe4-bitnumber0b1100?§ Underlineddigit=MSB§ Voteathttp://pollev.com/pbjones
A. 0b00001100B. 0b10001100C. 0b11111100D. 0b11001100E. We’relost…
20
CSE351, Summer 2020L05: Integers II, Floating Point I
SignExtension
v Task: Givena𝑤-bitsignedintegerX,convertitto𝑤+𝑘-bitsignedintegerX′ withthesamevalue
v Rule: Add 𝑘 copiesofsignbit§ Let𝑥" bethe𝑖-th digitofX inbinary§ X# = 𝑥$%&, … , 𝑥$%&, 𝑥$%&, 𝑥$%', … , 𝑥&, 𝑥(
21
𝑘 copiesofMSB
• • •X
Xʹ • • • • • •
• • •
𝑤
𝑘 𝑤
originalX
CSE351, Summer 2020L05: Integers II, Floating Point I
SignExtensionExample
v Convertfromsmallertolargerintegraldatatypesv Cautomaticallyperformssignextension
§ Javatoo
22
short int x = 12345;int ix = (int) x; short int y = -12345;int iy = (int) y;
Var Decimal Hex Binaryx 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
CSE351, Summer 2020L05: Integers II, Floating Point I
PracticeQuestion
v Assumeweareusing8-bitarithmetic:§ x == (unsigned char) x
§ x >= 128U
§ x != (x>>2)<<2
§ x == -x• Hint:therearetwosolutions
§ (x < 128U) && (x > 0x3F)
23
Forthefollowingexpressions,findavalueofsigned char x,ifthereexistsone,thatmakestheexpressionTRUE.Comparewithyourneighbor(s)!
CSE351, Summer 2020L05: Integers II, Floating Point I
Aside:UnsignedMultiplicationinC
v StandardMultiplicationFunction§ Ignoreshighorder𝑤 bits
v ImplementsModularArithmetic§ UMultw(u ,v)=u ·v mod2w
24
• • •
• • •
u
v*
• • •u · v
• • •
TrueProduct:𝟐𝒘 bits
Operands:𝒘 bits
Discard𝑤 bits:𝒘 bits
UMultw(u , v)
• • •
CSE351, Summer 2020L05: Integers II, Floating Point I
Aside:Multiplicationwithshiftandaddv Operationu<<k givesu*2k
§ Bothsignedandunsigned
v Examples:§ u<<3 == u * 8§ u<<5 - u<<3 == u * 24
§ Mostmachinesshiftandaddfasterthanmultiply• Compilergeneratesthiscodeautomatically
25
• • •u
2k*u · 2kTrueProduct:𝒘+ 𝒌 bits
Operands:𝒘 bits
Discard𝑘 bits:𝒘 bits UMultw(u , 2k)
0 0 1 0 0 0••• •••k
• • • 0 0 0•••
TMultw(u , 2k)0 0 0••••••
CSE351, Summer 2020L05: Integers II, Floating Point I
NumberRepresentationRevisited
v Whatcanwerepresentsofar?§ SignedandUnsignedIntegers§ Characters(ASCII)§ Addresses
v Howdoweencodethefollowing:§ Realnumbers(e.g. 3.14159)§ Verylargenumbers(e.g. 6.02×1023)§ Verysmallnumbers(e.g. 6.626×10-34)§ Specialnumbers(e.g.∞,NaN)
26
FloatingPoint
CSE351, Summer 2020L05: Integers II, Floating Point I
FloatingPointTopics
v Fractionalbinarynumbersv IEEEfloating-pointstandardv Floating-pointoperationsandroundingv Floating-pointinC
v Therearemanymoredetailsthatwewon’tcover§ It’sa58-pagestandard…
27
CSE351, Summer 2020L05: Integers II, Floating Point I
RepresentationofFractions
v “BinaryPoint,”likedecimalpoint,signifiesboundarybetweenintegerandfractionalparts:
Example6-bitrepresentation:
v Example: 10.10102 =1×21 +1×2-1 +1×2-3 =2.62510
28
xx.yyyy21 20 2-1 2-2 2-3 2-4
CSE351, Summer 2020L05: Integers II, Floating Point I
RepresentationofFractions
v “BinaryPoint,”likedecimalpoint,signifiesboundarybetweenintegerandfractionalparts:
Example6-bitrepresentation:
v Inthis6-bitrepresentation:§ Whatistheencodingandvalueof
thesmallest(mostnegative)number?
§ Whatistheencodingandvalueofthelargest(mostpositive)number?
§ Whatisthesmallestnumbergreaterthan2thatwecanrepresent?
29
xx.yyyy21 20 2-1 2-2 2-3 2-4
CSE351, Summer 2020L05: Integers II, Floating Point I
• • •b–1.
FractionalBinaryNumbers
v Representation§ Bitstorightof“binarypoint”representfractionalpowersof2§ Representsrationalnumber:
bi bi–1 b2 b1 b0 b–2 b–3 b–j• • •• • •124
2i–12i
• • •
1/21/41/8
2–j
30
CSE351, Summer 2020L05: Integers II, Floating Point I
FractionalBinaryNumbers
v Value Representation§ 5and3/4§ 2and7/8§ 47/64
v Observations§ Shiftleft=multiplybypowerof2§ Shiftright=dividebypowerof2§ Numbersoftheform0.111111…2 arejustbelow1.0
§ 1/2+1/4+1/8+…+1/2i +…➙ 1.0§ Usenotation1.0– ε
101.11210.11120.1011112
31
CSE351, Summer 2020L05: Integers II, Floating Point I
LimitsofRepresentation
v Limitations:§ Evengivenanarbitrarynumberofbits,canonlyexactlyrepresentnumbersoftheformx*2y (ycanbenegative)
§ Otherrationalnumbershaverepeatingbitrepresentations
Value: BinaryRepresentation:• 1/3=0.333333…10 = 0.01010101[01]…2
• 1/5= 0.001100110011[0011]…2
• 1/10= 0.0001100110011[0011]…2
32
CSE351, Summer 2020L05: Integers II, Floating Point I
Fixed PointRepresentation
v Impliedbinarypoint. Twoexampleschemes:#1:thebinarypointisbetweenbits2and3
b7 b6 b5b4 b3 [.] b2 b1 b0#2:thebinarypointisbetweenbits4and5
b7 b6 b5 [.] b4 b3 b2 b1 b0v Whereverweputthebinarypoint,withfixedpointrepresentationsthereisatradeoffbetweentheamountofrangeandprecisionwehave
v Fixedpoint=fixedrange andfixedprecision§ range:differencebetweenlargestandsmallestnumberspossible§ precision:smallestpossibledifferencebetweenanytwonumbers
v Hardtopickhowmuchyouneedofeach!33
CSE351, Summer 2020L05: Integers II, Floating Point I
Floating PointRepresentation
v Analogoustoscientificnotation§ InDecimal:
• Not12000000,but 1.2x107 InC:1.2e7• Not0.0000012,but 1.2x10-6 InC:1.2e-6
§ InBinary:• Not11000.000,but1.1x24
• Not0.000101,but1.01x2-4
v Wehavetodivvyupthebitswehave(e.g.,32)among:§ thesign(1bit)§ themantissa(significand)§ theexponent
34
CSE351, Summer 2020L05: Integers II, Floating Point I
ScientificNotation(Decimal)
v Normalizedform:exactlyonedigit(non-zero)toleftofdecimalpoint
v Alternativestorepresenting1/1,000,000,000§ Normalized: 1.0×10-9
§ Notnormalized: 0.1×10-8,10.0×10-10
35
6.0210 × 1023
radix(base)decimalpoint
exponentmantissa
CSE351, Summer 2020L05: Integers II, Floating Point I
ScientificNotation(Binary)
v Computerarithmeticthatsupportsthiscalledfloatingpoint duetothe“floating”ofthebinarypoint§ DeclaresuchvariableinCasfloat (ordouble)
36
1.012 × 2-1
radix(base)binarypoint
exponentmantissa
CSE351, Summer 2020L05: Integers II, Floating Point I
ScientificNotationTranslationv Convertfromscientificnotationtobinarypoint
§ Performthemultiplicationbyshiftingthedecimaluntiltheexponentdisappears• Example:1.0112×24 =101102 =2210• Example:1.0112×2-2 =0.010112 =0.3437510
v Convertfrombinarypointtonormalized scientificnotation§ Distributeoutexponentsuntilbinarypointistotherightofasingledigit
• Example:1101.0012 =1.1010012×23
v Practice:Convert11.37510 tonormalizedbinaryscientificnotation
37
CSE351, Summer 2020L05: Integers II, Floating Point I
Summary
v SignandunsignedvariablesinC§ Bitpatternremainsthesame,justinterpreted differently§ Strangethingscanhappenwithourarithmeticwhenweconvert/castbetweensignandunsignednumbers• Typeofvariablesaffectsbehaviorofoperators(shifting,comparison)
v Wecanonlyrepresentsomanynumbersin𝑤 bits§ Whenweexceedthelimits,arithmeticoverflow occurs§ Signextension triestopreservevaluewhenexpanding
v Floatingpointapproximatesrealnumbers§ WewilldiscussmoredetailsonMonday!
38
CSE351, Summer 2020L05: Integers II, Floating Point I
Someexamplesofusingshiftoperatorsincombinationwithbitmasks,whichyoumayfindhelpfulforLab1.
v Extractthe2nd mostsignificantbyteofanintv Extractthesignbitofasignedintv ConditionalsasBooleanexpressions
39
CSE351, Summer 2020L05: Integers II, Floating Point I
UsingShiftsandMasks
v Extractthe2nd mostsignificantbyte ofanint:§ Firstshift,thenmask:(x>>16) & 0xFF
§ Orfirstmask,thenshift:(x & 0xFF0000)>>16
40
0xFF 00000000 00000000 00000000 11111111
(x>>16) & 0xFF 00000000 00000000 00000000 00000010
x>>16 00000000 00000000 00000001 00000010
x 00000001 00000010 00000011 00000100
x & 0xFF0000 00000000 00000010 00000000 00000000
(x&0xFF0000)>>16 00000000 00000000 00000000 00000010
0xFF0000 00000000 11111111 00000000 00000000
x 00000001 00000010 00000011 00000100
CSE351, Summer 2020L05: Integers II, Floating Point I
UsingShiftsandMasks
v Extractthesignbitofasignedint:§ Firstshift,thenmask:(x>>31) & 0x1
• Assumingarithmeticshifthere,butthisworksineithercase• Needmasktoclear 1spossiblyshiftedin
41
x 00000001 00000010 00000011 00000100
x>>31 00000000 00000000 00000000 00000000
0x1 00000000 00000000 00000000 00000001
(x>>31) & 0x1 00000000 00000000 00000000 00000000
x 10000001 00000010 00000011 00000100
x>>31 11111111 11111111 11111111 11111111
0x1 00000000 00000000 00000000 00000001
(x>>31) & 0x1 00000000 00000000 00000000 00000001
0
0
11
CSE351, Summer 2020L05: Integers II, Floating Point I
UsingShiftsandMasks
v ConditionalsasBooleanexpressions§ Forint x,whatdoes(x<<31)>>31 do?
§ Canuseinplaceofconditional:• InC:if(x) {a=y;} else {a=z;} equivalenttoa=x?y:z;• a=(((x<<31)>>31)&y) | (((!x<<31)>>31)&z);
42
x=!!123 00000000 00000000 00000000 00000001
x<<31 10000000 00000000 00000000 00000000
(x<<31)>>31 11111111 11111111 11111111 11111111
!x 00000000 00000000 00000000 00000000
!x<<31 00000000 00000000 00000000 00000000
(!x<<31)>>31 00000000 00000000 00000000 00000000