Number systems in other bases.
Objectives
We would like to :- Examine number systems in different bases.
Notes
- In the last notes we discussed our base ten positional number system.
- I am really not interested in positionless number systems, or even semi-positional number systems (think Roman numerals)
- But I am interested in the base 10.
- What if we were in the world of Futurama?
- Consider the episode "The Devil's Hands Are Idle Playthings"
- In this episode the Robot Devil and Fry exchange hands.
- It looks like Fry's hand has three fingers and a thumb. (find a picture on line)
- And the Robot devil's "hand" has three fingers.
- I think this is true throughout the series.
- I also think it is common for cartoons to have less than 4 fingers and a thumb.
- Let's assume four digits per hand.
- In the naive counting on your hand system we could have the following
Fingers Value f 1 ff 2 fff 3 ffft 4 ffftf 5 ffftff 6 ffftfff 7 ffftffft 8 - Note we would then be out of "digits" so we would have to start over again.
- How would counting as we discussed in the last set of notes work? (Don't use your pinky.)
- This is a base 8 system.
- We will need to do a switch, remember on our fingers in base 10 we went from 0 to 9.
- In this system we will go from 0 to 7.
- Or use the digits {0, 1, 2, 3, 4, 5, 6, 7}
- In the naive counting on your hand system we could have the following
- Just a note, if we have a number without a subscript, it is base 10, for ALL other systems will will need to write a subscript.
- OR IT IS WRONG.
- So count to 15 in base 8.
- $0_8, 1_8, 2_8, 3_8, 4_8, 5_8, 6_8, 7_8$
- But the next number is 8 (base 10).
- But we don't have a symbol for 8 in base 8, so what do we do?
- Naive approach, $10_8$, because that is what we did in base 10?
- $10_8 = 1x8^1 + 0x8^0$ = 8 + 0 = 8 (base 10)
- What is $11_8$,
- naive - 9 because it comes after 8.
- $11_8 = 1x8^1 + 1x8^0$ = 8+1 = 9
- So continuing on $10_8 = 8, 11_8 = 9, 12_8 = 10, 13_8 = 11, 14_8 = 12, 15_8 = 13, 16_8 = 14, 17_8 = 15$
- But what about 16, that is 2x8 so 16 = $20_8$
- Base 8 is not special, nor is base 10.
- We are more comfortable in base 10.
- We will use base 8, somewhat, later.
- How about the Robot Devil?
- He has six digits total, so he might naively use base 6.
- digits = {0,1,2,3,4,5}
- What does $10_6$ represent?
- How about $15_6$
- How about $21_6$
- In general if we are in base b,
- Digits = {0, 1, 2, 3, ... b-1}
- If we need digits past 9, we generally use a, b, c, d, ...
- $a_{16} = 10, f_{16} = 16$
- ${a_{n-1}a_{n-2}...a_0}_b = a_{n-1}xb^{n-1} + a_{n-2}xb^{n-2} + ... + a_1b^1+a_0b^0$
- $21341_5 = 2x5^4 + 1x5^3 + 3x5^2 + 4x5^1 + 1x5^0$
- $532_7 = 5 x 7^2 + 3x7^1 + 2x7^0$
- Digits = {0, 1, 2, 3, ... b-1}
- For any system like this we need the digits and the base.
- We are interested in
- Base 10, as that is what we work in
- Base 2, as that is what most computers work in
- Base 8, because it is an easy shorthand for base 2
- Base 16, because it is an easy shorthand for base 2.
- By the way, we learned an algorithm in these notes that you need to know.
- To convert a number in any base to base 10
CONVERT-TO-BASE-10 Input: a string of digits S and an integer base b Assume s[n-1] is the most significant digit and s[0] is the least. Output: an integer- let n = length of S
- let sum = 0
- for i = n-1 to 0
- sum = sum + s[i] * pow(base, i)
- return sum
- To convert a number in any base to base 10