NumberSystems
1
2
NumberRepresentation
BinaryHexadecimalDecimal
3
TopicstobeDiscussed
• Howarenumericdataitemsactuallystoredincomputermemory?
• Howmuchspace(memorylocations)isallocatedforeachtypeofdata?– int,float,char,double,etc.
• Howarecharactersandstringsstoredinmemory?– Alreadydiscussed.
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
5
BinaryNumberSystem
• Twodigits:– 0and1.– Everydigitpositionhasaweightwhichisapowerof2.– Base orradix is2.
• Example:
110=1x22 +1x21 +0x20
101.01=1x22 +0x21 +1x20 +0x2-1 +1x2-2
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
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
8
Decimal-to-BinaryConversion
• Considertheintegerandfractionalpartsseparately.• Fortheintegerpart,
– Repeatedlydividethegivennumberby2,goonaccumulating theremainders,untilthenumberbecomeszero.
– Arrangetheremaindersinreverseorder.
• Forthefractionalpart,– Repeatedlymultiplythegivenfractionby2.
• Accumulatetheintegerpart(0or1).• Iftheintegerpartis1,chopitoff.
– Arrangetheintegerpartsintheorder theyareobtained.
9
Example1::239
2 2392119--- 12 59--- 1229--- 12 14--- 127--- 02 3--- 121--- 120--- 1
(239)10 =(11101111)2
2 64232--- 02 16--- 028--- 02 4--- 022--- 02 1--- 020--- 1
10
Example2::64
(64)10 =(1000000)2
11
Example3::.634
.634x2=1.268
.268x2=0.536
.536x2=1.072
.072x2=0.144
.144x2=0.288::
(.634)10 =(.10100……)2
12
Example4::37.0625
(37)10 =(100101)2
(.0625)10 =(.0001)2
∴ (37.0625)10 =(100101.0001)2
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
14
Binary-to-HexadecimalConversion
• Fortheintegerpart,– Scanthebinarynumberfromrighttoleft.– Translateeachgroupoffourbitsintothecorrespondinghexadecimaldigit.• Addleading zerosifnecessary.
• Forthefractionalpart,– Scanthebinarynumberfromlefttoright.– Translateeachgroupoffourbitsintothecorrespondinghexadecimaldigit.• Addtrailing zerosifnecessary.
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
16
Hexadecimal-to-BinaryConversion
• Translateeveryhexadecimaldigitintoits4-bitbinaryequivalent.– Discardleadingandtrailingzerosifdesired.
• Examples:(3A5)16 =(001110100101)2(12.3D)16 =(00010010.00111101)2(1.8)16 =(0001.1000)2
17
NumberRepresentation
UnsignedandSignednumbers
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)
19
SignedIntegerRepresentation
• Manyofthenumericaldataitemsthatareusedinaprogramaresigned(positiveornegative).– Question::Howtorepresentsign?
• Threepossibleapproaches:a)Sign-magnituderepresentationb)One’scomplementrepresentation
c) Two’scomplementrepresentation
20
Sign-magnitudeRepresentation
• Forann-bitnumberrepresentation– Themostsignificantbit(MSB)indicatessign
0à positive1à negative
– Theremainingn-1bitsrepresentmagnitude.
b0b1bn-2bn-1
MagnitudeSign
21
Example::n=4
0000à +00001à +10010à +20011à +30100à +40101à +50110à +60111à +7
1000à -01001à -11010à -21011à -31100à -41101à -51110à -61111à -7
15distinctnumberscanberepresented
22
Contd.
• Rangeofnumbersthatcanberepresented:Maximum::+(2n-1 – 1)Minimum::– (2n-1 – 1)
• Aproblem:Twodifferentrepresentationsofzero.
+0 à 0 000….0–0 à 1 000….0
23
One’sComplementRepresentation
• Basicidea:– Positivenumbersarerepresentedexactlyasinsign-magnitudeform.
– Negativenumbersarerepresentedin1’scomplementform.
• Howtocomputethe1’scomplementofanumber?– Complementeverybitofthenumber(1à0and0à1).– MSBwillindicatethesignofthenumber.
0à positive
1à negative
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
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)
26
Two’sComplementRepresentation
• Basicidea:– Positivenumbersarerepresentedexactlyasinsign-magnitudeform.
– Negativenumbersarerepresentedin2’scomplementform.
• Howtocomputethe2’scomplementofanumber?– Complementeverybitofthenumber(1à0and0à1),andthenadd1totheresultingnumber.
– MSBwillindicatethesignofthenumber.0à positive
1à negative
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
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.
29
Contd.
• InC(typicalvalues):– shortint
• 16bitsè +(215–1)to–215
– int• 32bitsè +(231–1)to–231
– longint• 64bitsè +(263–1)to–263
30
Binaryoperations
AdditionSubtractionusingaddition
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
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.
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
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
35
SubtractionUsingAddition::2’sComplement
• HowtocomputeA– B?– Computethe2’scomplementofB(say,B2).
– ComputeR=A+B2
– Ifthecarryobtainedafteradditionis‘1’• Ignorethecarry.
• Theresultisapositivenumber.
Else• Theresultisnegative,andisin2’scomplementform.
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
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
38
Floating-pointnumberrepresentation
39
Floating-pointNumbers• Therepresentationsdiscussedsofarappliesonlyto
integers.– Cannotrepresentnumberswithfractionalparts.
• Wecanassumeadecimalpointbeforea2’scomplementnumber.– Inthatcase,purefractions(withoutintegerparts)canbe
represented.
• Wecanalsoassumethedecimalpointsomewhereinbetween.– Thislacksflexibility.– Verylargeandverysmallnumberscannotberepresented.
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
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
42
AWarning
• Therepresentationforfloating-pointnumbersasshownisjustforillustration.
• Theactualrepresentationisalittlemorecomplex.
• InC:– float::32-bitrepresentation– double::64-bitrepresentation
43
RepresentationofCharactersandStrings
44
RepresentationofCharacters
• Manyapplications havetodealwithnon-numericaldata– Charactersandstrings.– Theremustbeastandardmechanismtorepresentalphanumericand
othercharactersinmemory.
• Threestandards inuse:– ExtendedBinaryCodedDecimalInterchangeCode(EBCDIC)
• UsedinolderIBMmachines.
– AmericanStandardCodeforInformationInterchange(ASCII)• Mostwidelyusedtoday.
– UNICODE• Usedtorepresentallinternationalcharacters.• UsedbyJava.
45
ASCIICode
• Eachindividualcharacterisnumericallyencodedintoaunique7-bitbinarycode.– Atotalof27 or128differentcharacters.
– Acharacterisnormallyencodedinabyte(8bits),withtheMSBnotbeenused.
• Thebinaryencodingofthecharactersfollowaregularordering.– Digitsareorderedconsecutivelyintheirpropernumericalsequence
(0to9).– Letters(uppercaseandlowercase)arearrangedconsecutivelyin
theirproperalphabeticorder.
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)
47
CharacterStrings
• Twowaysofrepresentingasequenceofcharactersinmemory.– Thefirstlocationcontainsthenumberofcharactersinthestring,followedbytheactualcharacters.
– Thecharactersfollowoneanother,andisterminatedbyaspecialdelimiter.
oeH5 ll
⊥leH ol
48
StringRepresentationinC
• InC,thesecondapproachisused.– The‘\0’characterisusedasthestringdelimiter.
• Example:“Hello”à
• Anullstring“”occupiesonebyteinmemory.– Onlythe‘\0’character.
‘\0’leH ol