Place value system Division Algorithm Basis Representation Theorem Convert between bases
Basis Representation Theorem
Ha Thanh Bac
March 22, 2024
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
1 Place value system
2 Division Algorithm
3 Basis Representation Theorem
4 Convert between bases
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Non place value system
In a non place value system, also called a nonpositional
system, the placement of the digits in a number does not
change its value.
Example
In Roman numerals, the symbol V always means ”five”
whether it occurs last in a numeral string (e.g., XXV), next to
last (XXVI), third from last (XXVII) or fourth from last
(XXVIII).
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Place value system
In contrast, a place value system, also called a positional
system, is a numeration system in which the placement of the
digits in a number determines the value of the digits.
Example
In the usual number 73, the 7 represents seven tens and the 3
represents three ones. However, in 37, the 3 would represent
three tens and the 7 would represent seven ones.
In daily life, we’re working with base 10 place value system.
Example
Standard form: 12345
Expanded form: 1.104 + 2.103 + 3.102 + 4.101 + 5.100
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Division Algorithm
Theorem
For any positive integers a and b, b ̸= 0, there exists a unique
pair (q, r ) of nonnegative integers such that
b = aq + r , where 0 ≤ r < a.
In this theorem, q is called the quotient and r is called the
remainder.
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Division Algorithm
If b < a, then q = 0 and r = b < a.
If b = a, then q = 1 and r = 0 < a.
If b > a, then there exist positive integers q such that
qa ≤ b < (q + 1)a. Let r = b − aq. It follows that b = aq + r
and 0 ≤ r < a.
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Division Algorithm (cont’d)
For the uniqueness, assume that b = aq ′ + r ′ , where q ′ and r ′
are also nonnegative integers satisfying 0 ≤ r ′ < a. Then
aq + r = aq ′ + r ′ , implying a(q − q ′ ) = r ′ − r , and so a|r ′ − r .
Hence, |r ′ − r | ≥ a or |r ′ − r | = 0.
Because 0 ≤ r , r ′ < a yields |r ′ − r | < a, we are left with
|r ′ − r | = 0, implying r ′ = r and consequently, q ′ = q.
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Basis Representation Theorem
Theorem
Let b > 1 a natural number. Every natural number n has a
unique expansion
n = ak b k + ak−1 b k−1 + · · · + a1 b + a0 , (1)
where 0 ≤ ai < b, ak ̸= 0 and k is the least number with
n < b k+1 .
Relation (1) is called the base b representation of n and is
denoted by
n = (ak ak−1 . . . a0 )b .
Some important bases: base 2 (binary), base 8 (octal), base
16 (hexadecimal).
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Basis Representation Theorem
For the existence, we repeatedly apply the Division Algorithm:
n = bq1 + a0 , 0 ≤ a0 ≤ b − 1.
If q1 ̸= 0, continue by dividing q1 by b
q1 = bq2 + a1 , 0 ≤ a1 ≤ b − 1.
Continue this process while qi ̸= 0
q2 = bq3 + a2 , 0 ≤ a2 ≤ b − 1,
q3 = bq4 + a3 , 0 ≤ a3 ≤ b − 1,
...
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Basis Representation Theorem (cont’d)
We can see that n > q1 > q2 > · · · ≥ 0 is a strictly decrease
sequence of nonnegative integers, so there exists k such that
qk < b. Then
...
qk−1 = bqk + ak−1 , 0 ≤ ak−1 ≤ b − 1,
qk = b.0 + ak , 0 < ak ≤ b − 1.
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Basis Representation Theorem (cont’d)
Let q0 = n. Then
k k−1
ai b i = (qi − qi+1 b)b i + qk b k
X X
i=0 i=0
k−1 k−1
qi b i − qi+1 b i+1 + qk b k
X X
=
i=0 i=0
k k
i
qi b i = q0 = n
X X
= q0 + qi b −
i=0 i=0
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Basis Representation Theorem (cont’d)
Now we prove that k is the least number with n < b k+1 or in
other words, k is the largest number with b k ≤ n.
Assume there exists a number h > k and b h ≤ n, then
n ≥ b h ≥ b k+1 , but because ai ≤ b − 1, so
n = a0 +a1 b+· · ·+ak b k ≤ (b−1)(1+b+· · ·+b k ) = b k+1 −1 < b k+1 ,
which is a contradiction.
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Basis Representation Theorem (cont’d)
For the uniqueness, assume that n = c0 + c1 b + · · · + ch b h is
another such representation.
If h ̸= k, for example, h > k, then
n = c0 + c1 b + · · · + ch b h ≥ ch b h ≥ b h ≥ b k+1 ,
but
n = a0 +a1 b+· · ·+ak b k ≤ (b−1)(1+b+· · ·+b k ) = b k+1 −1 < b k+1 ,
which is a contradiction. We can do similarly with the case
h < k.
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Proof of Basis Representation Theorem (cont’d)
If h = k, then
a 0 + a 1 b + · · · + a k b k = c0 + c1 b + · · · + ck b k
⇔a0 − c0 = (c1 − a1 )b + · · · + (ck − ak )b k
h i
⇔a0 − c0 = b (c1 − a1 ) + · · · + (ck − ak )b k−1
and so b|a0 − c0 . On the other hand, 0 ≤ a0 , c0 < b yields
|a0 − c0 | < b, hence a0 − c0 = 0 or a0 = c0 . Therefore
a1 + a2 b + · · · + ak b k−1 = c1 + c2 b + · · · + ck b k−1 .
Repeating the procedure above, it follows that a1 = c1 ,
a2 = c2 , . . . , ak = ck .
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases
The BR theorem and its proof give us the way to convert a
number from base 10 to the other base and vice versa.
Example
To find the expansion of 214 base 3, we do the following:
214 = 3.71 + 1
71 = 3.23 + 2
23 = 3.7 + 2
7 = 3.2 + 1
2 = 3.0 + 2
Take the remainders and we get 214 = (21221)3 .
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases
Example
In hexadecimal (base 16), A = 10, B = 11, C = 12, D = 13,
E = 14, F = 15. To convert (BC 123)16 to decimal number,
we do the following:
(BC 123)16 = 11.164 + 12.163 + 1.162 + 2.161 + 3.160
= 770339
To convert between abitrary bases, the simplest way is to go
through base 10 and then convert to the other base
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code
Firstly, we define some helper functions
1 # Function to return equivalent number
2 # of a character
3 def val ( c ) :
4 if ( c >= ’0 ’ and c <= ’9 ’) :
5 return ord ( c ) - 48
6 else :
7 return ord ( c ) - 65 + 10
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
1 # Function to return equivalent
2 # character of a given value
3 def reVal ( num ) :
4 if ( num >= 0 and num <= 9) :
5 return chr ( num + 48)
6 else :
7 return chr ( num - 10 + 65)
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
Then define a function to convert a number from a given base
to decimal number
1 def toDeci ( strr , base ) :
2 # Stores the length of the string
3 lenn = len ( strr )
4 # Initialize power of base
5 power = 1
6 # Initialize result
7 num = 0
8 # Expression string to print the calculation
9 expression = strr + " _ " + str ( base ) + " = "
10 for i in range ( lenn - 1 , -1 , -1) :
11 # A digit in input number must < base
12 if ( val ( strr [ i ]) >= base ) :
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
13 print ( " Invalid Number " )
14 return -1
15 # Update num
16 num += val ( strr [ i ]) * power
17 # Update power
18 power = power * base
19 # Update expression
20 expression += f " { strr [ i ]}*{ base }ˆ0 " if
power == 1 else f " { strr [ i ]}*{ base }ˆ{ power //
base } "
21 if i != 0:
22 expression += " + "
23 expression += f " = { num } "
24 print ( expression )
25 return num
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
Define another function which converts a given decimal
number to a given base
1 def fromDeci ( base , inputNum ) :
2 # Store the result
3 res = " "
4 # Repeatedly divide inputNum by base and take
5 # remainder
6 while ( inputNum > 0) :
7 # Get the quotient and remainder
8 quotient = inputNum // base
9 remainder = inputNum % base
10 # Update res
11 res += reVal ( remainder )
12 # Update inputNum
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
13 inputNum = quotient
14 # Print the step
15 print ( f " { inputNum } = { quotient }*{ base } + {
remainder } " )
16 # Reverse the result
17 res = res [:: -1]
18 return res
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
The main function utilizing all functions above to convert
number from one base to another base
1 def convertBase (s , a , b ) :
2 # Convert the number from
3 # base A to decimal
4 print ( f " Convert { s } _ { a } to decimal : " )
5 num = toDeci (s , a )
6 # Convert the number from
7 # decimal to base B
8 print ( f " Convert result to base -{ b }: " )
9 ans = fromDeci (b , num )
10 # Print the result
11 print ( f " Final result : { s } _ { a }={ ans } _ { b } " )
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
Let’s try an example: convert octal number 258 to binary. We
simply call the function
1 convertBase ( " 25 " , 8 , 2)
Ha Thanh Bac
Basis Representation Theorem
Place value system Division Algorithm Basis Representation Theorem Convert between bases
Convert between bases - Python code (cont’d)
and get the result as follows
Convert 25_8 to decimal:
25_8 = 5*8ˆ0 + 2*8ˆ1 = 21
Convert result to base-2:
21 = 10*2 + 1
10 = 5*2 + 0
5 = 2*2 + 1
2 = 1*2 + 0
1 = 0*2 + 1
Final result: 25_8=10101_2
Ha Thanh Bac
Basis Representation Theorem