2’s Complement and Floating-Point - University of …...Two’s Complement •An n-bit, two’s...
Transcript of 2’s Complement and Floating-Point - University of …...Two’s Complement •An n-bit, two’s...
2’sComplementandFloating-Point
CSE351Section3
Two’sComplement
•Ann-bit,two’scomplementnumbercanrepresenttherange[−2 𝑛−1 ,2𝑛−1 −1].
• Notetheasymmetryofthisrangeabout0– there’sonemorenegativenumberthanpositive
•Notewhathappenswhenyouoverflow
4-bittwo’scomplementrange
UnderstandingTwo’sComplement
• Aneasierwaytofindthedecimalvalueofatwo’scomplementnumber:
~x + 1 = -x
• Wecanrewritethisasx=~(-x- 1),i.e.subtract1fromthegivennumber,andflipthebitstogetthepositiveportionofthenumber.
• Example:0b11010110• Subtract1:0b11010110 - 1 = 0b11010101• Flipthebits:~0b11010101 = 0b00101010 • Converttodecimal: 0b00101010 = (32+8+2)10 = 4210• Multiplybynegativeone,Answer: -4210.
Two’sComplement:Operations
1. Convertnumberstobinary
• 0xAB = 0b10101011
• 1710 = 0b00010001
2. Computex | y 0000 ... 1010 1011
| 0000 ... 0001 0001--------------------0000 ... 1011 1011
3. Compute~(x | y) (flipthebits)~ 0000 0000 0000 0000 0000 0000 1011 1011-----------------------------------------1111 1111 1111 1111 1111 1111 0100 0100
int x = 0xAB;int y = 17;int z = 5;int result = ~(x | y) + z;
Whatisthevalueofresultindecimal?
Two’sComplement:Operations4. Compute ~(x | y) + z
• Youcaneitherconverttodecimalbeforeorafteraddingz
• 1710 = 0b00010001
• Convert111111111111111111111111010001002 to decimal(hint: -x = ~x + 1)
• ~111111111111111111111111010001002 = 0000000000000000000000000101110112
• ~0000……101110112 + 1 = 0000……101111002
• -x = 0000……101111002
• x = 1000……101111002
int x = 0xAB;int y = 17;int z = 5;int result = ~(x | y) + z;
Whatisthevalueofresultindecimal?
FloatingPoint
• value=(-1)S*M *2E
• NumericalForm• Signbits determineswhethernumberisnegativeorpositive• Significand(mantissa)M normallyafractionalvalueinrange[1.0,2.0)• ExponentE weightsvaluebya(possiblynegative)poweroftwo
• RepresentationinMemory• MSBsissignbits• exp fieldencodesE (butisnotequaltoE)– rememberthebias• Frac fieldencodesM (butisnotequaltoM)
s exp mant
FloatingPoint
• value=(-1)S*M *2E
• Value:±1× Mantissa× 2Exponent
• Bit Fields:(-1)S × 1.M × 2(E+bias)
• Bias• Readexponentasunsigned,butwithbiasof–(2w-1-1)=–127• Representableexponentsroughly½positiveand½negative• Exponent0(Exp =0)isrepresentedasE=0b01111111
• Why?• Floatingpointarithmetic=easier• Somewhatcompatiblewith2’scomplement
s exp mant
FloatingPoint
• Exponentoverflowyields+∞or-∞• Floatswithvalue+∞,-∞,andNaN canbeusedinoperations• Resultusuallystill+∞,-∞,orNaN;sometimesintuitive,sometimesnot
• Floatingpointopsdonotworklikerealmath,duetorounding!• Notassociative:
• (3.14+1e100)– 1e100!=3.14+(1e100– 1e100)• Notdistributive:
• 100*(0.1+0.2)!=100*0.1+100*0.2• Notcumulative
• Repeatedlyaddingaverysmallnumbertoalargeonemaydonothing
s exp mant
FloatingPointExamples
• Howdoyourepresent-1.5infloatingpoint?• Signbit:1• Firsttheintegralpartofthevalue:1=0b1• Nowcomputethedecimal:0.5=0b0.1• 1.510 =1.1b• Don’tneedtonormalizebecauseit’salreadyinscientificnotation:1.1x20• Exponent:0+127=12710 =011111112• Mantissa:10000000000000000000002• -1.510=10111111110000000000000000000002 =0xBFC00000
s exp mant
1 8 23
FloatingPointExamples
• Howdoyourepresent1.0infloatingpoint?• Signbit:0• Firsttheintegralpartofthevalue:1=0b1• 1.010 =>1.0b• Don’tneedtonormalizebecauseit’salreadyinscientificnotation:1.0x20• Exponent:0+127=12710 =011111112• Mantissa:000000000000000000000002• 1.010=001111111000000000000000000000002 =0x3F800000
s exp mant
1 8 23
FloatingPointAddition
(–1)s1*M1*2E1 + (-1)s2*M2*2E2
(AssumeE1 >E2)• ExactResult:(–1)s*M*2E
• Signs,mantissaM:• M=M1+M2,resultofsignedalign&add
• ExponentE:E1
• Fixing• If M ≥ 2, shift M right, increment E• if M < 1, shift M left k positions, decrement E by k• OverflowifE outofrange• RoundM tofitfrac precision
s exp mant
1 8 23
Whatisfloatingpointresultof12.3125+1.5?12.312510 =0 10000010 100010100000000000000001.510 =0 01111111 10000000000000000000000
Inscientific notation:(1.1000101x23)+(1.1x 20 )1100.0101 x 20
+ 1.1 x 20
------------------
1101.1101 x 20
Adjusted mantissa:1.1011101=>M=1011101Ans:0 10000010 10111010000000000000000
FloatingPointMultiplication
(–1)s1*M1*2E1 * (–1)s2*M2*2E2
• ExactResult:(–1)s*M*2E• Signs: s1 ^ s2• MantissaM: M1 * M2• ExponentE: E1 + E2
• Fixing• IfM ≥ 2,shiftM right,incrementE• IfE outofrange,overflow• RoundM tofitfrac precision
s exp mant
1 8 23
Whatisfloatingpointresultof1.1*1.0?1.110 =0 01111111 000110011001100110011011.010 =0 01111111 00000000000000000000000
Inscientific notation:(1.1x20)+(1.0x 20 )1.00011001100110011001101 x 20
* 1.0 x 20
--------------------------------------00000000000000000000000001000110011001100110011010
Result: 1.000110011001100110011010 x 20
Ans:0 01111111 00011001100110011001101
FloatingPointWorksheet