Java Reference
In-Depth Information
Oneobjectiontostoringeachvalueinanindividualvariableisthe
wastedspace.Whenwedevoteanintvariableforstoringthenumberof
printersconnectedtothesystem,wearewastingconsiderablestorage
space.Anintvariableconsistsoffourmemorybytes(referto
Table4-1
).
Thismeansthatyoucanstoreover2millioncombinationsinaninttype.
However,inthisparticularexamplethemaximumnumberofprintersis
3.Youcoulduseavariableoftypebyteorshortbuttherewouldstillbe
considerablewaste.Thesameappliestoallotherdatatypespreviously
listed.
Amoreeconomicaloption,memorywise,wouldbetodevotetoeach
itemtheminimumamountofstoragenecessaryforencodingallpossible
states.Inthecaseofthenumberofprintersyoucoulddothiswithjust
twobits.Twobitsallowrepresentingvaluesfrom0to3,whichissuffi-
cientforthisdataelement.Bythesametoken,asinglebitwouldserveto
recordifamouseispresentornot.Theconventioninthiscaseisthata
binary1representsYESandabinary0representsNO.Thenumberofse-
rialports(range0to5)couldbeencodedinathree-bitfield,whilean-
othersinglebitwouldrecordthepresenceorabsenceofMMX
technologyinthePentiumCPU.Thetotalstoragewouldbeasfollows:
•
printers,2bits
•
mousepresent,1bit
•
serialports,3bits
•
MMXpresent,1bit
Incidentally...
TheMultimediaExtension(MMX)isamodificationofthearchitec-
tureofthePentiumCPUusedinthePC.TheMMXprovidesanex-
tendedsetofinstructionsthatfacilitateprogrammingandenhance
theperformanceofgraphics,multimedia,andotherhigh-perfor-
manceapplications.
Thetotalstoragerequiredissevenbits.
Figure8-1
(onthenextpage)
shows how the individual bits of a byte variable can be assigned to store
this information.