number systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/l7-number.pdf · 5 binary number...
TRANSCRIPT
![Page 1: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/1.jpg)
NumberSystems
1
![Page 2: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/2.jpg)
2
NumberRepresentation
BinaryHexadecimalDecimal
![Page 3: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/3.jpg)
3
TopicstobeDiscussed
• Howarenumericdataitemsactuallystoredincomputermemory?
• Howmuchspace(memorylocations)isallocatedforeachtypeofdata?– int,float,char,double,etc.
• Howarecharactersandstringsstoredinmemory?– Alreadydiscussed.
![Page 4: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/4.jpg)
4
NumberSystem::TheBasics
• Weareaccustomedtousingtheso-calleddecimalnumbersystem.– Tendigits::0,1,2,3,4,5,6,7,8,9– Everydigitpositionhasaweightwhichisapowerof10.– Base orradix is10.
• Example:
234 =2x102 +3x101 +4x100
250.67=2x102 +5x101 +0x100 +6x10-1 +7x10-2
![Page 5: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/5.jpg)
5
BinaryNumberSystem
• Twodigits:– 0and1.– Everydigitpositionhasaweightwhichisapowerof2.– Base orradix is2.
• Example:
110=1x22 +1x21 +0x20
101.01=1x22 +0x21 +1x20 +0x2-1 +1x2-2
![Page 6: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/6.jpg)
6
Binary-to-DecimalConversion
• Eachdigitpositionofabinarynumberhasaweight.– Somepowerof2.
• Abinarynumber:B=bn-1 bn-2 …..b1 b0 .b-1 b-2 …..b-m
Correspondingvalueindecimal:
D=Σ bi 2ii=-m
n-1
![Page 7: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/7.jpg)
7
Examples
1. 101011à 1x25 +0x24 +1x23 +0x22 +1x21 +1x20
=43(101011)2 =(43)10
2. .0101à 0x2-1 +1x2-2 +0x2-3 +1x2-4
=.3125(.0101)2 =(.3125)10
3. 101.11à 1x22 +0x21 +1x20 +1x2-1 +1x2-2
5.75(101.11)2 =(5.75)10
![Page 8: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/8.jpg)
8
Decimal-to-BinaryConversion
• Considertheintegerandfractionalpartsseparately.• Fortheintegerpart,
– Repeatedlydividethegivennumberby2,goonaccumulating theremainders,untilthenumberbecomeszero.
– Arrangetheremaindersinreverseorder.
• Forthefractionalpart,– Repeatedlymultiplythegivenfractionby2.
• Accumulatetheintegerpart(0or1).• Iftheintegerpartis1,chopitoff.
– Arrangetheintegerpartsintheorder theyareobtained.
![Page 9: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/9.jpg)
9
Example1::239
2 2392119--- 12 59--- 1229--- 12 14--- 127--- 02 3--- 121--- 120--- 1
(239)10 =(11101111)2
![Page 10: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/10.jpg)
2 64232--- 02 16--- 028--- 02 4--- 022--- 02 1--- 020--- 1
10
Example2::64
(64)10 =(1000000)2
![Page 11: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/11.jpg)
11
Example3::.634
.634x2=1.268
.268x2=0.536
.536x2=1.072
.072x2=0.144
.144x2=0.288::
(.634)10 =(.10100……)2
![Page 12: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/12.jpg)
12
Example4::37.0625
(37)10 =(100101)2
(.0625)10 =(.0001)2
∴ (37.0625)10 =(100101.0001)2
![Page 13: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/13.jpg)
13
HexadecimalNumberSystem
• Acompactwayofrepresentingbinarynumbers.
• 16differentsymbols(radix=16).0à 0000 8à 10001à 0001 9à 10012à 0010 Aà 10103à 0011 Bà 1011
4à 0100 Cà 11005à 0101 Dà 11016à 0110 Eà 1110
7à 0111 Fà 1111
![Page 14: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/14.jpg)
14
Binary-to-HexadecimalConversion
• Fortheintegerpart,– Scanthebinarynumberfromrighttoleft.– Translateeachgroupoffourbitsintothecorrespondinghexadecimaldigit.• Addleading zerosifnecessary.
• Forthefractionalpart,– Scanthebinarynumberfromlefttoright.– Translateeachgroupoffourbitsintothecorrespondinghexadecimaldigit.• Addtrailing zerosifnecessary.
![Page 15: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/15.jpg)
15
Examples
1. (1011 0100 0011)2 =(B43)16
2. (10 1010 0001)2 =(2A1)16
3. (.1000 010)2 =(.84)16
4. (101 .0101 111)2 =(5.5E)16
![Page 16: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/16.jpg)
16
Hexadecimal-to-BinaryConversion
• Translateeveryhexadecimaldigitintoits4-bitbinaryequivalent.– Discardleadingandtrailingzerosifdesired.
• Examples:(3A5)16 =(001110100101)2(12.3D)16 =(00010010.00111101)2(1.8)16 =(0001.1000)2
![Page 17: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/17.jpg)
17
NumberRepresentation
UnsignedandSignednumbers
![Page 18: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/18.jpg)
18
UnsignedBinaryNumbers
• Ann-bitbinarynumberB=bn-1bn-2 ….B2b1b0
(2n distinctcombinationsarepossible,0to2n−1)
• Forn=3,thereare8distinctcombinations.– 000,001,010,011,100,101,110,111
• Rangeofnumbersthatcanberepresentedn=8 è 0to28−1(255)n=16 è 0to216−1(65535)n=32 è 0to232−1(4294967295)
![Page 19: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/19.jpg)
19
SignedIntegerRepresentation
• Manyofthenumericaldataitemsthatareusedinaprogramaresigned(positiveornegative).– Question::Howtorepresentsign?
• Threepossibleapproaches:a)Sign-magnituderepresentationb)One’scomplementrepresentation
c) Two’scomplementrepresentation
![Page 20: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/20.jpg)
20
Sign-magnitudeRepresentation
• Forann-bitnumberrepresentation– Themostsignificantbit(MSB)indicatessign
0à positive1à negative
– Theremainingn-1bitsrepresentmagnitude.
b0b1bn-2bn-1
MagnitudeSign
![Page 21: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/21.jpg)
21
Example::n=4
0000à +00001à +10010à +20011à +30100à +40101à +50110à +60111à +7
1000à -01001à -11010à -21011à -31100à -41101à -51110à -61111à -7
15distinctnumberscanberepresented
![Page 22: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/22.jpg)
22
Contd.
• Rangeofnumbersthatcanberepresented:Maximum::+(2n-1 – 1)Minimum::– (2n-1 – 1)
• Aproblem:Twodifferentrepresentationsofzero.
+0 à 0 000….0–0 à 1 000….0
![Page 23: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/23.jpg)
23
One’sComplementRepresentation
• Basicidea:– Positivenumbersarerepresentedexactlyasinsign-magnitudeform.
– Negativenumbersarerepresentedin1’scomplementform.
• Howtocomputethe1’scomplementofanumber?– Complementeverybitofthenumber(1à0and0à1).– MSBwillindicatethesignofthenumber.
0à positive
1à negative
![Page 24: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/24.jpg)
24
Example::n=4
0000à +00001à +10010à +20011à +30100à +40101à +50110à +60111à +7
1000à -71001à -61010à -51011à -41100à -31101à -21110à -11111à -0
Tofindtherepresentationof,say,-4,firstnotethat
+4=0100
−4=1’scomplementof0100=1011
![Page 25: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/25.jpg)
25
Contd.• Rangeofnumbersthatcanberepresented:
Maximum::+(2n-1 – 1)Minimum::– (2n-1 – 1)
• Aproblem:Twodifferentrepresentationsofzero.
+0à 0000….0−0à 1111….1
• Advantageof1’scomplementrepresentation– Subtractioncanbedoneusingaddition.– Leadstosubstantial savingincircuitry.– Signextensionispossibletoincreasenumberofbitstorepresent.
-3=1100(in4bits)=11111100(in8bits)+3=0011(in4bits)=00000011(in8bits)
![Page 26: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/26.jpg)
26
Two’sComplementRepresentation
• Basicidea:– Positivenumbersarerepresentedexactlyasinsign-magnitudeform.
– Negativenumbersarerepresentedin2’scomplementform.
• Howtocomputethe2’scomplementofanumber?– Complementeverybitofthenumber(1à0and0à1),andthenadd1totheresultingnumber.
– MSBwillindicatethesignofthenumber.0à positive
1à negative
![Page 27: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/27.jpg)
27
Example::n=4
0000à +00001à +10010à +20011à +30100à +40101à +50110à +60111à +7
1000à -81001à -71010à -61011à -51100à -41101à -31110à -21111à -1
Tofindtherepresentationof,say,-4,firstnotethat
+4=0100
−4=2’scomplementof0100=1011+1=1100
![Page 28: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/28.jpg)
28
Contd.• Rangeofnumbersthatcanberepresented:
Maximum::+(2n-1 – 1)Minimum::– 2n-1
• Advantage:– Uniquerepresentationofzero.– Subtractioncanbedoneusingaddition.– Leadstosubstantial savingincircuitry.– Signextensionispossibletoincreasenumberofbitstorepresent.
-3=1101(in4bits)=11111101(in8bits)+3=0011(in4bits)=00000011(in8bits)
• Almostallcomputerstodayusethe2’scomplementrepresentationforstoringnegativenumbers.
![Page 29: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/29.jpg)
29
Contd.
• InC(typicalvalues):– shortint
• 16bitsè +(215–1)to–215
– int• 32bitsè +(231–1)to–231
– longint• 64bitsè +(263–1)to–263
![Page 30: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/30.jpg)
30
Binaryoperations
AdditionSubtractionusingaddition
![Page 31: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/31.jpg)
Binaryaddition
• Rulesforaddingtwobits:0+0=00+1=11+0=11+1=10,i.e.,0withcarryof1
• Additionexamplesforunsignednumbers:0 1 0 1 0 1 1 1 1 0 0 10 0 0 1 0 0 1 1 1 0 1 0======= ======= =========0 1 1 0 1 0 1 0 1 0 0 1 1
31
Carry
![Page 32: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/32.jpg)
32
SubtractionUsingAddition::1’sComplement
• HowtocomputeA– B?– Computethe1’scomplementofB(say,B1).
– ComputeR=A+B1
– Ifthecarryobtainedafteradditionis‘1’• AddthecarrybacktoR(calledend-aroundcarry).
• Thatis,R=R+1.
• Theresultisapositivenumber.
Else• Theresultisnegative,andisin1’scomplementform.
![Page 33: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/33.jpg)
33
Example1::6– 2
1’scomplementof2=1101
6 :: 0 1 1 0-2 :: 1 1 0 1
1 0 0 1 11
0 1 0 0 à +4
End-aroundcarry
Assume4-bitrepresentations.
Sincethereisacarry,itisaddedbacktotheresult.
Theresultispositive.
RB1A
![Page 34: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/34.jpg)
34
Example2::3– 5
1’scomplementof5=1010
3 :: 0 0 1 1-5 :: 1 0 1 0
1 1 0 1 Assume4-bitrepresentations.
Sincethereisnocarry,theresultisnegative.
1101isthe1’scomplementof0010,thatis,itrepresents–2.
AB1
R
-2
![Page 35: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/35.jpg)
35
SubtractionUsingAddition::2’sComplement
• HowtocomputeA– B?– Computethe2’scomplementofB(say,B2).
– ComputeR=A+B2
– Ifthecarryobtainedafteradditionis‘1’• Ignorethecarry.
• Theresultisapositivenumber.
Else• Theresultisnegative,andisin2’scomplementform.
![Page 36: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/36.jpg)
36
Example1::6– 2
2’scomplementof2=1101+1=1110
6 :: 0 1 1 0-2 :: 1 1 1 0
1 0 1 0 0
Assume4-bitrepresentations.
Presenceofcarryindicatesthattheresultispositive.
Noneedtoaddtheend-aroundcarrylikein1’scomplement.
AB2
R
Ignorecarry +4
![Page 37: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/37.jpg)
37
Example2::3– 5
2’scomplementof5=1010+1=1011
3 :: 0 0 1 1-5 :: 1 0 1 1
1 1 1 0 Assume4-bitrepresentations.
Sincethereisnocarry,theresultisnegative.
1110isthe2’scomplementof0010,thatis,itrepresents–2.
AB2
R
-2
![Page 38: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/38.jpg)
38
Floating-pointnumberrepresentation
![Page 39: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/39.jpg)
39
Floating-pointNumbers• Therepresentationsdiscussedsofarappliesonlyto
integers.– Cannotrepresentnumberswithfractionalparts.
• Wecanassumeadecimalpointbeforea2’scomplementnumber.– Inthatcase,purefractions(withoutintegerparts)canbe
represented.
• Wecanalsoassumethedecimalpointsomewhereinbetween.– Thislacksflexibility.– Verylargeandverysmallnumberscannotberepresented.
![Page 40: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/40.jpg)
40
RepresentationofFloating-PointNumbers• Afloating-pointnumberFisrepresentedbyadoublet<M,E>:
F=MxBE
• B: exponentbase(usually2)• M: mantissa• E:exponent
– Misusuallyrepresentedin2’scomplementform,withanimplieddecimalpointbeforeit.
• Forexample,Indecimal,
0.235x106
Inbinary,0.101011x20110
![Page 41: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/41.jpg)
41
Example::32-bitrepresentation
– Mrepresentsa2’scomplementfraction1>M>−1
– Erepresentstheexponent(in2’scomplementform)127>E>−128
• Pointstonote:– Thenumberofsignificantdigits dependsonthenumberofbits
inM.• 6significantdigitsfor24-bitmantissa.
– Therange ofthenumberdependsonthenumberofbitsinE.• 1038 to10−38 for8-bitexponent.
M E
24 8
![Page 42: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/42.jpg)
42
AWarning
• Therepresentationforfloating-pointnumbersasshownisjustforillustration.
• Theactualrepresentationisalittlemorecomplex.
• InC:– float::32-bitrepresentation– double::64-bitrepresentation
![Page 43: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/43.jpg)
43
RepresentationofCharactersandStrings
![Page 44: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/44.jpg)
44
RepresentationofCharacters
• Manyapplications havetodealwithnon-numericaldata– Charactersandstrings.– Theremustbeastandardmechanismtorepresentalphanumericand
othercharactersinmemory.
• Threestandards inuse:– ExtendedBinaryCodedDecimalInterchangeCode(EBCDIC)
• UsedinolderIBMmachines.
– AmericanStandardCodeforInformationInterchange(ASCII)• Mostwidelyusedtoday.
– UNICODE• Usedtorepresentallinternationalcharacters.• UsedbyJava.
![Page 45: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/45.jpg)
45
ASCIICode
• Eachindividualcharacterisnumericallyencodedintoaunique7-bitbinarycode.– Atotalof27 or128differentcharacters.
– Acharacterisnormallyencodedinabyte(8bits),withtheMSBnotbeenused.
• Thebinaryencodingofthecharactersfollowaregularordering.– Digitsareorderedconsecutivelyintheirpropernumericalsequence
(0to9).– Letters(uppercaseandlowercase)arearrangedconsecutivelyin
theirproperalphabeticorder.
![Page 46: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/46.jpg)
46
SomeCommonASCIICodes
‘A’::41(H) 65(D)‘B’::42(H) 66(D)………..‘Z’::5A(H) 90(D)
‘a’::61(H) 97(D)‘b’::62(H) 98(D)………..‘z’::7A(H) 122(D)
‘0’::30(H) 48(D)‘1’::31(H) 49(D)………..‘9’::39(H) 57(D)
‘(‘::28(H) 40(D)‘+’::2B(H) 43(D)‘?’::3F(H) 63(D)‘\n’::0A(H) 10(D)‘\0’::00(H) 00(D)
![Page 47: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/47.jpg)
47
CharacterStrings
• Twowaysofrepresentingasequenceofcharactersinmemory.– Thefirstlocationcontainsthenumberofcharactersinthestring,followedbytheactualcharacters.
– Thecharactersfollowoneanother,andisterminatedbyaspecialdelimiter.
oeH5 ll
⊥leH ol
![Page 48: Number Systemscse.iitkgp.ac.in/~saptarshi/courses/pdstheory2019a/L7-Number.pdf · 5 Binary Number System • Two digits: – 0 and 1. – Every digit position has a weight which is](https://reader034.vdocuments.us/reader034/viewer/2022051921/600e68d118234d3b33055deb/html5/thumbnails/48.jpg)
48
StringRepresentationinC
• InC,thesecondapproachisused.– The‘\0’characterisusedasthestringdelimiter.
• Example:“Hello”à
• Anullstring“”occupiesonebyteinmemory.– Onlythe‘\0’character.
‘\0’leH ol