Upload
dinhmien
View
219
Download
2
Embed Size (px)
Citation preview
DataRepresentation,Types&Conversions
RepresentationSchemes• UnsignedMagnitude• SignedMagnitude• One’scomplement• Two’scomplement
Unsignedmagnitude• Unsignedintegerscanrepresentzeroandposi9veintegers,butnotnega9veintegers.• Example1:Supposethatn=8andthebinarypaFernis01000001,thevalueofthisunsignedintegeris1×2^0+1×2^6=65.• Example2:Supposethatn=16andthebinarypaFernis0001000000001000,thevalueofthisunsignedintegeris1×2^3+1×2^12=4104.• Ann-bitpaFerncanrepresent2^ndis9nctintegers.• Ann-bitunsignedintegercanrepresentintegersfrom0to(2^n)-1
Signedmagnitude• Themost-significantbit(msb)isthesignbit,withvalueof0represen9ngposi9veintegerand1represen9ngnega9veinteger.
• Theremainingn-1bitsrepresentsthemagnitude(absolutevalue)oftheinteger.Theabsolutevalueoftheintegerisinterpretedas"themagnitudeofthe(n-1)-bitbinarypaFern".
Signedmagnitude• Example1:Supposethatn=8andthebinaryrepresenta9onis01000001.Signbitis0⇒posi9veAbsolutevalueis1000001=65Hence,theintegeris+65
• Example2:Supposethatn=8andthebinaryrepresenta9onis10000001.Signbitis1⇒nega9veAbsolutevalueis0000001=1Hence,theintegeris-1
Signedmagnitude• Example3:Supposethatn=8andthebinaryrepresenta9onis00000000.Signbitis0⇒posi9veAbsolutevalueis0000000=0Hence,theintegeris+0
• Example4:Supposethatn=8andthebinaryrepresenta9onis10000000.Signbitis1⇒nega9veAbsolutevalueis0000000B=0Hence,theintegeris-0
One’sComplement• Themostsignificantbit(msb)isthesignbit,withvalueof0represen9ngposi9veintegersand1represen9ngnega9veintegers.• Theremainingn-1bitsrepresentsthemagnitudeoftheinteger,asfollows:• forposi9veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthe(n-1)-bitbinarypaFern".• fornega9veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthecomplement(inverse)ofthe(n-1)-bitbinarypaFern"(hencecalled1'scomplement).
One’scomplement• Example1:Supposethatn=8andthebinaryrepresenta9on01000001.Signbitis0⇒posi9veAbsolutevalueis1000001=65Hence,theintegeris+65
• Example2:Supposethatn=8andthebinaryrepresenta9on10000001.Signbitis1⇒nega9veAbsolutevalueisthecomplementof0000001B,i.e.,1111110B=126Hence,theintegeris-126
Two’scomplement• Again,themostsignificantbit(msb)isthesignbit,withvalueof0represen9ngposi9veintegersand1represen9ngnega9veintegers.• Theremainingn-1bitsrepresentsthemagnitudeoftheinteger,asfollows:• forposi9veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthe(n-1)-bitbinarypaFern".• fornega9veintegers,theabsolutevalueoftheintegerisequalto"themagnitudeofthecomplementofthe(n-1)-bitbinarypaFernplusone"(hencecalled2'scomplement).
Two’scomplement• Example1:Supposethatn=8andthebinaryrepresenta9on00000000.Signbitis0⇒posi9veAbsolutevalueis0000000=0Hence,theintegeris+0
• Example2:Supposethatn=8andthebinaryrepresenta9on11111111.Signbitis1⇒nega9veAbsolutevalueisthecomplementof1111111Bplus1,i.e.,0000000+1=1Hence,theintegeris-1
Conversions• 8bit=1byte• 1024byte=1KB• 1024KB=1MB• 1024MB=1GB• 1024GB=1TB• 1024TB=1PB
• Kilo(10^3),Mega(10^6),Giga(10^9),Tera(10^12),Peta(10^15),Exa(10^18)…
• ClassExamples:• 2048bytes=……KB=…….Bit=……MB
Conversions•
DataTypes• Adatatypedefinesasetofvaluesthatavariablecanstorealongwithasetofopera8onsthatcanbeperformedonthatvariable.
• Commondatatypesareinteger,character,andreal.
DataTypes
DataTypes
DataTypes• BasicDatatype(Primary,fundamental)• Integers,CharacterandFloa8ngpoint
DataTypes• BasicDatatype(Primary,fundamental)• Integers• Signedandunsignedtypes
• Signed–canstore+and–veintegers• Unsigned–canstoreonly+veintegers
DataTypes• BasicDatatype(Primary,fundamental)• Signedtypeintegers• int:-integersarewholenumbers,capabletostoring
numericvaluewithoutdecimalplaces.• anynumberintherange-32768to32767(16-bit)• Itoccupies2bytesofmemory(16-bit),4bytes(32-bit)• Longint:-required4bytesofmemory(16-bit),8byte
(32-bit).• Valuerangefrom-2147483648to2147483647(32-bit)• Longintvariablecandeclare• longinta,b;orlonga;
DataTypes• BasicDatatype(Primary,fundamental)• Signedtypeintegers• Shortintegers:-needlessspaceinmemory(sameas
int)• Shortintvariablecandelare• shortinta;orinta;(botharesame)
DataTypes• BasicDatatype(Primary,fundamental)• Unsignedintegers• unsignedintegers:-some8meifweknowinadvanced,
thevaluestoredinanintegervariableisalwaysbe+ve.• Suchsitua8onswecandeclaredthevariableas
unsignedint• TherangepermissibleintegervaluewillshiWfrom0to
65535iedoublethesizeofint• Unsignedintegervariablecandeclare• unsignedinta;orunsigneda;(botharesame)
DataTypes• BasicDatatype(Primary,fundamental)• Unsignedintegers• unsignedshortintegers:-sameasunsignedint• unsignedlongintegers:-• Range0to42949672954(doublesizeoflongint)• Unsignedlongintegervariablecandeclare• unsignedlonginta;
DataTypes• BasicDatatype(Primary,fundamental)• Characters• Signedandunsignedtypes• Bothoccupy1byteofmemory• Buthavingdifferentrange• Signedcharissameasordinarycharandhasrange-128
to127• Unsignedcharrangefrom0to255• Example• cnsignedchara;• chara;
DataTypes• BasicDatatype(Primary,fundamental)• Floa8ngpoint
• Afloatvariableoccupy4bytesofmemory• Rangefrom3.4E-38to3.4E+38• Doubleoccupy8bytesofmemory• Rangefrom1.7E-308to1.7E+308• Longdoubleoccupy10bytesofmemory• Rangefrom3.4E-4932to3.4E+4932
DataTypes(16-bit)
DataTypes(32-bit)
MemoryAllocationofVariables
DataTypes• UserDefinedDataType• TypeDefini8on• Enumerateddatatype• Structure• Union
DataTypes• Deriveddatatype• Array…• Func8ons…• Pointers…• Reference…
DataTypes• Emptydatatype• void
EscapeSequenceEscapesequenceisusedintheprina()func9ontodosomethingtotheoutput.
DataRepresentation• Dataondigitalcomputersisrepresentedasasequenceof0sand1s.Thisincludesnumericdata,text,executablefiles,images,audio,andvideo.
• Datacanberepresentedusing2n• Numericrepresenta9on• Fixedpoint• Floa9ngpoint
• Nonnumericrepresenta9on
Drawbacks• Thedrawbacksofsign-magnituderepresenta9onare:• Therearetworepresenta9ons(00000000Band10000000B)forthenumberzero,whichcouldleadtoinefficiencyandconfusion.
• Posi9veandnega9veintegersneedtobeprocessedseparately.
Review• Char:8bits• int:32bits(Itactuallydependsonmicroprocessor&opera9ngsystem)
• Float:Usually32bits• Double:64bits