Numbering systems

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Contents

Introduction

You may have been wondering what the deal is with that "1" and "0" thing that IT guy down the hall who waves his hands around when he talks says "Computers communicate in." Or maybe you have heard of a "Hex Editor" and you are waiting for some heavenly message to tell you what the "Hex" in that is. Or maybe you have heard a little bit more of these "Numbering Systems" (Maybe it's not completely hopeless...) and you just found this page in hope of a further explanation. Or perhaps, you just found this article and it looked absolutely, positively spiffy, so you decided to dive in. You've come to the right place, even if you wanted change for a $10100 (in binary).

Silly stuff aside (as if), lets get to the very, very, intense definitions. A numbering system is just that. A system of making and interpreting symbols that mean a quantity.

You may not think of it under the same terms of other numbering systems that you are not familiar with, but according to our definition, the normal base 10, 0-9, Arabic number system you are familiar with is a numbering system, called "decimal." By our definition, something like the tally system is also a numbering system. We'll call systems like the tally system "one for one systems." This comes from the way there is one symbol for one increase in quantity. Though I still believe in the definition previously defined, one for one systems are not usually enveloped by the commonly held definition for a numbering system. To tell you that one, I need to ramble a bit more (Sorry...).

Rambling and Math

Most of the modern numbering systems in use are number based, which I'll discuss later. The alternative to this is a one for one number system that is just, for example, a tally system where every increase in quantity is represented by a new symbol, which in the case of the tally system is a line (Except of course every fifth line, but that doesn't matter in our example.) These systems are extremely inefficient. Imaging writing a large number in a number system like this.

Lets say 5 in tallies takes up 1 square half inch (small strangely shaped tallies, but for the sake of simplicity...).<pulls out calculator and pokes it confusedly for a while>(no guarantee on these figures[ ; )])Thats like 8.33 sqr. ft. of paper for writing 1000 in tallies. There would either be 6318.18 square miles(33,360,000 sqr. ft.) of wasted paper or no census. Ouch. That is a lot. Wouldn't you prefer to write "4,000,000,000"?

That's where the cleverness of these number based number systems comes in. Lets take our native "decimal" for an example. This will all seem extremely obvious to you, but we're talking about theory here...

We have 10 symbols that stand for quantities

0, 1, 2, 3, 4, 5, 6, 7, 8, and 9

This works great for representing what we know of as single-digit numbers, but what happens with quantities greater than that?

Well, a long, long time ago, some extremely clever person first thought of the idea of a sequence of these symbols that gave a higher quantity value to symbols in different positions in the sequence. This is a what we'll call a number-based numbering system. Example Please!!

The quantity 1 = "1"....OK, this works with single digit numbers
  The quantity 2 = "2"
  The quantity 3 = "3"
  ........
  The quantity 7 = "7" 
  The quantity 8 = "8"
  The quantity 9 = "9"
But what happens with quantities higher than this? Observe
The quantity 10 = (a symbol with a single-digit value of 1  +  the symbol that stands for the quantity none)

Let's analyze this. We have a quantity of 10(in a quantity of O's this would be "OOOOOOOOOO" .) If we go with the idea of giving symbols in positions farther to the left higher value than they would have if they were to the right, then we could give a symbol in the second slot going left a value equal to the value of the symbol if it was in the first slot multiplied by the base of the numbering system(in the case of the decimal that would be 10.) the zero in the first slot is worth 0 immediately, and the 1 is worth 1 * 10. We add both those values together and we get 10. Thus 10 = 10. Big revalation, huh. Well I said this would seem obvious... That's It! We've conquered it! Now lets try it out!

56 in decimal
  the six is equal to the quantity 6 immediately
  the 5 is equal to 5 * 10 because we are in a base ten numbering system
  so, 56 = (5 * 10) + 6
  It makes sense(hopefully)! Booya!!

Now what happens with one more slot to the left? I'll save you the thought.

247 in decimal
  7 = quantity 7
  4 = 4 * 10 OK, we know that, but what about the next number?
  2 = 2 * 10 * 10
  It works!

I think it's due time for an straight equation for use in programming giving the value of the digit of a number!(I figured this out all by myself, just to let you know...)

where x = the digit's original value
  s = slot where the 0 in "10" would be 1 and the 1 would be 2
  b = base of numbering system; would be 10 in decimal
  y = the outputted value of the digit  
  y = x * b ^ (s - 1) (the * stands for multiplication and the ^ stands for "to the power of ")

Examples: lets find the value of the 6 in 23,735,269

y = 6 * 10 ^ (2 - 1)
  10 ^ 1 = 10 
  6 * 10 = 60
 It's right, isn't it. 
Let's try the 7
   y = 7 *10 ^ (6 - 1)
  10 ^ 5 = 100,000
  7 * 100,000 = 700,000
  Boo-Ya!!

Now let's see what happens when we have a numbering system with a base other than decimal. In this example we will use a base 12 numbering system. This numbering system, probably called "decibinary", is not really used, but for an example we will use it.

This system uses the symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, and B with the Arabic numbers equaling the quantity of their decimal cousins and the A equaling the quantity 10 with the B standing for 11. The decimal quantities 1 - 23 in this system would be like the following

1 2 3 4 5 6 7 8 9 A B 10 11 12 13 14 15 16 17 18 19 1A 1B

Let's use our little equation on the "1" in the decibinary quantity 1B

y =  1 * 12 ^ (2 - 1)
  y =  1 * 12 ^ 1
  y =  1 * 12
  y =  12

We then add this to the value of the "B" in that number

y = 11 * 12  ^ (1 - 1)
  y = 11 * 12  ^  0
  y = 11 * 1
  y = 11

So, "1B" would be equal to 12 + 11 = 23. If you count the number of numbers there are including the last "1B" in the list of 1 - 23 numbers in our "decibinary" system, it turns out to be the 23rd number.

Right again! I suppose it works (I came up with this equation off the top of my head, so I wouldn't really know if it works...) Now let's move on to more rambling, my head hurts from all this crazy math stuff, what with all the numbers and other numerical things...

Types of Numbering Systems

Decimal-Base 10, Digits: 0123456789

This is the commonly used numbering system that you use in everyday life. Its popularity could be due to several reasons. 1. This system is easier math and counting wise. We may be biased because we grew up using this, but working with tens is a lot easier than counting by, say 16 or 8 because to find the power of it all we have to do is add a zero. 2. The use of this system may have come easier to ancient people first beginning to use number systems because we have ten fingers, and ten toes which could be used for counting

Binary-Base 2, Digits: 01

The only place this language is used is a situation where the only available symbols are off and on, such as in a computer. I has no practical use aside from that. This takes more than twice (almost thrice!) the digits decimal does to make a number. To make 255:

In decimal: 255

In binary: 11111111

Hexadecimal-Base 16, Digits: 0123456789ABCDEF

The hexadecimal numbering system, usually known as hex, has a whopping 16 digits. If you look at decimal in comparison to binary, you can see that the higher the base, the more verbose the numbering system is in real live use. This applies here. Whereas in decimal, it takes three digits to write 255, that quantity is expressed in hex as "FF." This makes it useful in places where a gabby system takes up too much room, like in a hex editor. Hex is also sometimes used for the ease of converting a binary number to hex and vice-versa, which is beyond the scope of this tutorial.

Octal-Base 8, Digits: 01234567

Octal isn't really ever used anymore, so you really only need to know that it existed. It has been replaced in most of the places where it was with hex.