Post on 21-Apr-2018
Decimal(base10)
Digits={0,1,2,3,4,5,7,8,9}
Exampleofnumerals:11,923,5548,etc.
11 = 1 ∗ 10& + 1 ∗ 10(923 = 9 ∗ 10, + 2 ∗ 10& + 3 ∗ 10(
5548 = 5 ∗ 1005 ∗ 10, + 4 ∗ 10& + 8 ∗ 10(
𝑚 =2𝑑 𝑖 ∗ 105�
57(
digit
Binary(base2)
Bits={0,1}
Exampleofnumerals:11,101,1010,etc.
11 = 1 ∗ 2& + 1 ∗ 2(101 = 1 ∗ 2, + 0 ∗ 2& + 1 ∗ 2(
1010 = 1 ∗ 20 + 0 ∗ 2, + 1 ∗ 2& + 0 ∗ 2(
𝑚 =2𝑏 𝑖 ∗ 25�
57(
bit
Fixedsizerepresentation
Decimal Binary0 000000001 000000012 000000103 000000114 000001005 000001016 000001107 000001118 00001000
Fixednumberofbits(typically8,16,32,64…).
8bitsiscalled“byte”.
Conversion
• Howtoconvertfrombinarytodecimal?
11010 , = 1 ∗ 29 + 1 ∗ 20 + 0 ∗ 2, + 1 ∗ 2& + 0 ∗ 2( = 16 + 8 + 0 + 2 + 0 = 26
Note:Youneedtoknowpowersof2…
• Howtoconvertfromdecimaltobinary?
241 &( =? ? ?
Operationsondecimals
Usethispropertyofanypositiveinteger𝑚 ∶𝑚 = 𝑚%10 + 𝑚 10⁄ ∗ 10
(integer)divisionby10=droppingrightmostdigit
Ex:238/10=23
Multiplicationby10=shiftingleftbyonedigit
Ex:23*10=230
Remainderofintegerdivisionby10=rightmostdigit
Ex:238%10=8
OperationsonbinarySamepropertyholdsforbinary:
𝑚 = 𝑚%2 + 𝑚 2⁄ ∗ 2
Example:𝑚 = 1011 ,𝑚 2⁄ = 0101 ,
𝑚 2⁄ ∗ 2 = 1010 ,𝑚%2 = 0001 ,
Decimal⟶Binary (Algorithm)
Algorithm decimal2binary(m)Input:adecimalmOutput:abinarybi⟵0whilem>0do
b[i]⟵m%2m⟵m/2i⟵i+1
Decimal⟶Binary (Example)i m/2 m%2(b[i])
2410 120 11 60 02 30 03 15 04 7 15 3 16 1 17 0 18 - -
Answer:b[]=…011110001
Whyisthealgorithmworking?
𝑚 = 𝑚 2 ∗ 2 +𝑚%2⁄
…𝑏 3 𝑏 2 𝑏 1 ,
…𝑏 3 𝑏 2 𝑏 1 0 , 𝑏 0 ,
…𝑏 3 𝑏 2 𝑏 1 𝑏[0] ,
Additioninbinary
1 1 0 1 0 = 26+ 0 0 1 1 1 1 = 15= 1 0 1 0 0 1 = 41
2A 29 20 2, 2& 2(1 0 1 0 0 1
= 2A + 20 + 2( = 32 + 8 + 1 = 41
Operationinbinary
Recallgrade-schoolalgorithmforaddition,subtraction,multiplication,anddivision.
Thereisnothingspecialaboutbase10.
Thesealgorithmsworkforbinary(base2),andworkforotherbasestoo!
Representationsize
𝑚 = 2 𝑏 𝑖 ∗ 25BC&
57(
Whatistherelationshipbetween𝑚 andN?
(HowmanybitsNdoweneedtorepresentapositiveintegerm?)
Upperbound
225BC&
57(
= 1 + 2 + 4 +⋯+ 2BC& = 2B − 1
(Thisisaspecialcaseof∑ 𝑥5 = HIC&HC&
BC&57( where𝑥 = 2)
𝑚 = 2 𝑏 𝑖 ∗ 25BC&
57(
≤ 2 1 ∗ 25BC&
57( = 2B − 1 < 2B
log, 𝑚 < 𝑁 (applylogonbothsides)
Lowerbound
Wecanassumethat𝑁 −1istheindex𝑖 oftheleftmostbit𝑏[i]suchthat𝑏[𝑖]=1(weignoreleftmost0’s:00001101).
𝑚 = 2 𝑏 𝑖 ∗ 25BC&
57(
≥ 2BC&
log, 𝑚 ≥ 𝑁 − 1log, 𝑚 + 1 ≥ 𝑁
Howmanybitdoweneed?
log, 𝑚 < 𝑁 ≤ log, 𝑚 + 1Answer: Thelargestintegerlessthanorequaltolog, 𝑚 + 1.
Wewriteitas𝑁 = log, 𝑚 + 1 (a.k.a.”floor”thatmeans“rounddown”)
Examplesm(decimal) m (binary) 𝑁 = log, 𝑚 + 1
0 0 -1 1 12 10 23 11 24 100 35 101 36 110 37 111 48 1000 49 1001 4