2. Number Bases

2.1. Decimal (Base Ten)

  • The decimal number system is used in everyday life

  • The decimal number system is base ten; it consists of ten different symbols/numerals

    • \(0, 1, 2, 3, 4, 5, 6, 7, 8, 9\)

  • These symbols have an ordering to them

    • \(0 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5 \rightarrow 6 \rightarrow 7 \rightarrow 8 \rightarrow 9\)

  • These symbols also have a meaning/value associated with them

    • The symbol \(5\) means five

    • It conveys a magnitude

  • The magnitude of these values correspond to the ordering

    • \(0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9\)

2.1.1. Counting in Base Ten

  • Counting in base ten consists of moving to the next symbol

    Counting with Ten Symbols

    \(0\)

    Zero

    \(1\)

    One

    \(2\)

    Two

    \(3\)

    Three

    \(4\)

    Four

    \(5\)

    Five

    \(6\)

    Six

    \(7\)

    Seven

    \(8\)

    Eight

    \(9\)

    Nine

    \(????\)

    Ten?

  • However, there are only ten unique symbols

  • This means it’s not possible to count past nine with the symbols alone

  • This is where symbol position comes in

2.1.1.1. Position

  • First, consider that each number has an infinite number of zeros in front of it

    • Why this is will be discussed shortly

  • Consider the below table where only seven of the leading zeros are included

    • Thus, these numbers will each have a total of eight symbols

    • Each of these symbols/numerals, in this context, is called a digit

      • Digit comes from latin (digitus), meaning finger or toe, of which, humans typically have ten of each

    Values Expressed with Eight Digits

    \(00000000\)

    Zero

    \(00000001\)

    One

    \(00000002\)

    Two

    \(00000003\)

    Three

    \(00000004\)

    Four

    \(00000005\)

    Five

    \(00000006\)

    Six

    \(00000007\)

    Seven

    \(00000008\)

    Eight

    \(00000009\)

    Nine

  • To count past the value nine, a new rule is introduced

  • If the no more symbols are left

    • Reset the symbol to zero in the current position

    • Change to the subsequent symbol in the next position

    Counting with Digits

    \(00000008\)

    Eight

    \(00000009\)

    Nine

    \(00000010\)

    Ten

    \(00000011\)

    Eleven

    \(00000018\)

    Eighteen

    \(00000019\)

    Nineteen

    \(00000020\)

    Twenty

    \(00000099\)

    Ninety Nine

    \(00000100\)

    One Hundred

    \(00000101\)

    One Hundred and One

  • Consider the number \(101\)

  • Although this number contains two ones, they have a different meaning because of their position

2.1.1.2. Names

  • Each of these positions has a name

  • From right to left, they are

    Digit Position Names

    First Digit

    Ones

    \(10^{0}\)

    \(1\)

    Second Digit

    Tens

    \(10^{1}\)

    \(10\)

    Third Digit

    Hundreds

    \(10^{2}\)

    \(100\)

    Fourth Digit

    Thousands

    \(10^{3}\)

    \(1000\)

    Fifth Digit

    Ten Thousands

    \(10^{4}\)

    \(10000\)

    Sixth Digit

    Hundred Thousands

    \(10^{5}\)

    \(100000\)

    Seventh Digit

    Millions

    \(10^{6}\)

    \(1000000\)

    Eighth Digit

    Ten Millions

    \(10^{7}\)

    \(10000000\)

  • As a consequence of the counting pattern, each position corresponds to a value that is the base raised to some power

  • Consider the number \(123\)

  • The symbol \(1\) in the hundreds position is \(1 \times 10^{2}\)

    • There is one hundred

  • The symbol \(2\) in the tens position is \(2 \times 10^{1}\)

    • There are two tens

  • The symbol \(3\) in the ones position is \(3 \times 10^{0}\)

    • Three ones

  • Thus, the number is \(1 \times 10^{2} + 2 \times 10^{1} + 3 \times 10^{0}\)

  • It may feel strange to think about the number \(123\) like this

  • But this is what the decimal encoding of the number is conveying

../../_images/123_with_cash.png

The value \(123\) represented with Canadian Dollars. There is one hundred, two tens, and three ones.

2.1.1.3. Consequence of Finite Digits

  • Although with integers, there are an infinite number of leading zeros and positions

  • Only eight digits were used to encode the numbers in the above examples

  • Ths means there is a limit to the number of unique positive integer values that can be expressed

  • The largest number is \(99999999\)

    • Ninety nine million, nine hundred and ninety nine thousand, nine hundred and ninety nine

  • Obviously there are values larger than this, but they are not representable with only 8 digits

2.2. Binary (Base Two)

  • Decimal is the typical way numbers are encoded in every day life

  • However, base ten (decimal) is only an encoding

    • It’s not a number, it’s a way to represent a number

  • Other bases could just as easily be used

  • For example, binary (base two)

  • Instead of ten symbols, only two are used — \(0, 1\)

    • \(0\) means zero, like base ten

    • \(1\) means one, like base ten

  • The symbols have an ordering to the magnitude of values, like base ten

    • \(0 \rightarrow 1\)

    • \(0 < 1\)

  • Position matters, like base ten

    • The value of a \(1\) depends on where it is

  • There are an infinite number of leading zeros

    • \(1 == 00000001 == 0000000000000001\)

  • In decimal, the symbols are called digits

  • In binary, they are called bits

    • Binary information digit — bit

  • The counting rules are the same

    • Increment to the next symbol

    • If there are no more symbols

      • Reset to \(0\)

      • Increment the symbol in the next position

    Counting in Binary

    \(00000000\)

    Zero

    \(00000001\)

    One

    \(00000010\)

    Two

    \(00000011\)

    Three

    \(00000100\)

    Four

    \(00000101\)

    Five

    \(00000110\)

    Six

    \(00000111\)

    Seven

    \(00001000\)

    Eight

  • With eight bits, the largest positive integer that could be represented is \(255\)

    • Can represent the numbers 0 through to 255

  • Like base ten, the specific bit position carries different values

  • These values are always the base to some power

  • Although these bit positions don’t really go by specific names, they can be named like the digits

    Bit Position Names

    First Digit

    Ones

    \(2^{0}\)

    \(1\)

    Second Digit

    Twos

    \(2^{1}\)

    \(2\)

    Third Digit

    Fours

    \(2^{2}\)

    \(4\)

    Fourth Digit

    Eights

    \(2^{3}\)

    \(8\)

    Fifth Digit

    Sixteens

    \(2^{4}\)

    \(16\)

    Sixth Digit

    Thirty-twos

    \(2^{5}\)

    \(32\)

    Seventh Digit

    Sixty-fours

    \(2^{6}\)

    \(62\)

    Eighth Digit

    One hundred and twenty eights

    \(2^{7}\)

    \(128\)

  • Like base ten, the binary number can be broken down to the sum of its positional values

  • Consider the number \(1111011\)

    • One Sixty-four

    • One thirty-two

    • One sixteen

    • One eight

    • Zero fours

    • One two

    • One one

  • Thus, the number is

    \(1 \times 2^{6} + 1 \times 2^{5} + 1 \times 2^{4} + 1 \times 2^{3} + 0 \times 2^{2} + 1 \times 2^{1} + 1 \times 2^{0}\)

    \(1 \times 64 + 1 \times 32 + 1 \times 16 + 1 \times 8 + 0 \times 4 + 1 \times 2 + 1 \times 1\)

    \(64 + 32 + 16 + 8 + 0 + 2 + 1\)

    \(123\)

Note

Position really matters — have you ever counted to \(31\) on one hand?

Typically, when counting with fingers, the position of the finger is ignored. This means that the biggest number one could count to on one hand is five. This is effectively base 1.

However, it is possible to make use of the position of each finger to get more out of your hand. Try it yourself.

../../_images/finger_counting_binary_19.png

The number 19 in binary represented with fingers. The thumb is the least significant bit (ones) and the pinky finger is the most significant bit (sixteens).

2.3. Hexadecimal (Base 16)

  • Using a base larger than ten is fine

  • However, there are only ten conventional numerals

  • Because of this, additional symbols are needed to represent values greater than 9

  • Consider hexadecimal — base 16

    • Often called hex

  • Although any symbol could be used to represent the values 10 through 15, the letters \(A\) through \(F\) are used

  • Like counting in decimal and binary, the same rules apply to hexadecimal

  • The symbols have an ordering to their magnitudes

    • \(0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < A < B < C < D < E < F\)

  • Counting works the same way

    • Leading zeros also exist, but are excluded below for brevity

    Counting in Hexadecimal

    \(0\)

    Zero

    \(1\)

    One

    \(9\)

    Nine

    \(A\)

    Ten

    \(B\)

    Eleven

    \(C\)

    Twelve

    \(D\)

    Thirteen

    \(E\)

    Fourteen

    \(F\)

    Fifteen

    \(10\)

    Sixteen

    \(11\)

    Seventeen

    \(FF\)

    Two Hundred and Fifty Five

    \(100\)

    Two Hundred and Fifty Six

    \(101\)

    Two Hundred and Fifty Seven

  • Like binary, the hexadecimal digits don’t really have names, but they can be named to get a sense of their meaning

    Hexadecimal Digit Position Names

    First Digit

    Ones

    \(16^{0}\)

    \(1\)

    Second Digit

    Sixteens

    \(16^{1}\)

    \(16\)

    Third Digit

    Two Hundred and Fifty Sixes

    \(16^{2}\)

    \(255\)

    Fourth Digit

    Four thousand and Ninty Sixes

    \(16^{3}\)

    \(4096\)

  • Like decimal and binary, a hexadecimal number can be broken down into the sum of its positional values

  • Consider the number \(7B\)

    • Seven Sixteens

    • Eleven Ones

  • Thus, the number can be calculated as follows

    \(7 \times 16^{1} + 11 \times 16^{0}\)

    \(7 \times 16 + 11 \times 1\)

    \(112 + 11\)

    \(123\)

2.4. Converting Numbers Between Bases

  • Although only three bases were discussed so far, but there is nothing stopping one from using base 5, or 123, or …

  • A subscript is added to the number to specify the base when there is a potential for ambiguity

  • For example, consider the number \(10\)

    • What number is that?

    • Is it a base ten number?

    • Base two?

    • Hexadecimal?

  • Ambiguity is eliminated by including the subscript

    • \(10_{10}\) means ten

    • \(10_{2}\) means two

    • \(10_{16}\) means sixteen

  • In a computing context, binary and hexadecimal are often written as 0b10 and 0x10 respectively

2.4.1. Converting to Decimal

  • It is often useful to convert numbers from other bases to decimal since that is what is used in everyday life

    • Saying \(123\) is a lot easier for us to understand than saying \(1111011_{2}\) or \(7B_{16}\)

    • However, an encoding is only an encoding — these all mean the same thing

  • For both the binary and hexadecimal bases discussed, numbers were already converted to decimal

    • \(1111011_{2}\)

      \((1 \times 2^{6} + 1 \times 2^{5} + 1 \times 2^{4} + 1 \times 2^{3} + 0 \times 2^{2} + 1 \times 2^{1} + 1 \times 2^{0})_{10}\)

      \((1 \times 64 + 1 \times 32 + 1 \times 16 + 1 \times 8 + 0 \times 4 + 1 \times 2 + 1 \times 1)_{10}\)

      \((64 + 32 + 16 + 8 + 0 + 2 + 1)_{10}\)

      \(123_{10}\)

    • \(7B_{16}\)

      \((7 \times 16^{1} + 11 \times 16^{0})_{10}\)

      \((7 \times 16 + 11 \times 1)_{10}\)

      \((112 + 11)_{10}\)

      \(123_{10}\)

  • In general, multiply the value of the digit in base ten by the value of the base in base ten to the power of its corresponding position

  • For example, given some number of \(n\) digits \(d\) in base \(b\)

    \((d_{n-1}d_{n-2}...d_{2}d_{1}d_{0})_{b}\)

  • The value in base ten would be calculated as follows

    \((d_{n-1} \times b^{n-1} + d_{n-2} \times b^{n-2} + ... + d_{2} \times b^{2} + d_{1} \times b^{1} + d_{0} \times b^{0})_{10}\)

  • Consider the number \(J204_{20}\) — note that \(J_{20} == 19_{10}\) in this example

    \((19 \times 20^{3} + 2 \times 20^{2} + 0 \times 20^{1} + 4 \times 20^{0})_{10}\)

    \((19 \times 8000 + 2 \times 400 + 0 \times 20 + 4 \times 1)_{10}\)

    \((152000 + 800 + 0 + 4)_{10}\)

    \(152804_{10}\)

2.4.2. Converting from Decimal

  • To convert a number from decimal to some arbitrary base \(b\)

    • Divide the number by \(b\) and keep the quotient \(q_{0}\) and remainder \(r_{0}\)

    • The value of \(r_{0}\) is the digit for the least significant position

    • Divide \(q_{0}\) by the base \(b\) to get a new quotient \(q_{1}\) and remainder \(r_{1}\)

    • The value of \(r_{1}\) is the digit for the next position

    • Repeat until the quotient is zero

  • Consider converting the number \(123_{10}\) to binary

    Converting \(123_{10}\) to Binary

    Digit/Bit Position

    Division

    \(q\)

    \(r\)

    \(0\)

    \(123/2\)

    \(61\)

    \(1\)

    \(1\)

    \(61/2\)

    \(30\)

    \(1\)

    \(2\)

    \(30/2\)

    \(15\)

    \(0\)

    \(3\)

    \(15/2\)

    \(7\)

    \(1\)

    \(4\)

    \(7/2\)

    \(3\)

    \(1\)

    \(5\)

    \(3/2\)

    \(1\)

    \(1\)

    \(6\)

    \(1/2\)

    \(0\)

    \(1\)

  • Therefore, the number \(123_{10}\) is \(1111011_{2}\)

  • Consider converting the number \(123_{10}\) to hexadecimal

    Converting \(123_{10}\) to Hexadecimal

    Hex Digit Position

    Division

    \(q\)

    \(r\)

    \(0\)

    \(123/16\)

    \(7\)

    \(11_{10}\) or \(B_{16}\)

    \(1\)

    \(7/16\)

    \(0\)

    \(7\)

  • Therefore, the number \(123_{10}\) is \(7B_{2}\)

2.4.3. Converting Between Arbitrary Bases

  • Converting between arbitrary bases will not be covered in detail

    • If one really wants to convert between base, they can do it in two steps with decimal

    • For example, converting from base 5 to base 7

      • Convert from base five to decimal

      • Convert from decimal to base 7

  • However, there is an interesting trick when converting between bases when one is an even power of the other

  • Consider binary and hexadecimal

    • \(2^{4} == 16\)

  • Each group of four bits corresponds to a single hexadecimal digit

  • This is perhaps best illustrated with an example

    \(1111011_{2}\)

  • Take the least significant batch of four bits and convert to hexadecimal

    \(1011_{2} == 11_{10} == B_{16}\)

  • Take the next batch of four bits, add leading zeros if necessary, and convert to hexadecimal

    \(0111_{2} == 7_{10} == 7_{16}\)

  • One can then combine the hexadecimal digits to create the hexadecimal number

    \(1111011_{2} == 7B_{16}\)

  • This works because, with any grouping of four bits, sixteen unique values can be represented

    • \(0\) through \(15\)

  • With hexadecimal, sixteen unique values can be represented with a single hexadecimal digit

    • \(0\) through \(F\)

  • This works in general when one base is an even power of another

  • In the below table, observe that

    • Base four can be obtained by combining two bits from base two

    • Base eight can be obtained by combining three bits from base two

    • Base sixteen can be obtained by combining four bits from base two

    • Base sixteen can also be obtained by combing two base four digits

    Numbers in Base two, Four, Eight, and Sixteen

    Base Two

    Base Four

    Base Eight

    Base Sixteen

    \(0000\)

    \(00\)

    \(00\)

    \(0\)

    \(0001\)

    \(01\)

    \(01\)

    \(1\)

    \(0010\)

    \(02\)

    \(02\)

    \(2\)

    \(0011\)

    \(03\)

    \(03\)

    \(3\)

    \(0100\)

    \(10\)

    \(04\)

    \(4\)

    \(0101\)

    \(11\)

    \(05\)

    \(5\)

    \(0110\)

    \(12\)

    \(06\)

    \(6\)

    \(0111\)

    \(13\)

    \(07\)

    \(7\)

    \(1000\)

    \(20\)

    \(10\)

    \(8\)

    \(1001\)

    \(21\)

    \(11\)

    \(9\)

    \(1010\)

    \(22\)

    \(12\)

    \(A\)

    \(1011\)

    \(23\)

    \(13\)

    \(B\)

    \(1100\)

    \(30\)

    \(14\)

    \(C\)

    \(1101\)

    \(31\)

    \(15\)

    \(D\)

    \(1110\)

    \(32\)

    \(16\)

    \(E\)

    \(1111\)

    \(33\)

    \(17\)

    \(F\)

2.5. For Next Time