Thursday, October 19, 2023

How to Count and Convert Numbers

Regardless of how old you are, there is a chance you do not actually know how to count correctly. I decided I would post my guide to proper counting. The reason I say you might not know how to count is because counting in anything should be universal. Whether it is base 10, what we use on a day to day, base 8, base 2, or even base 16, it all works the same. So to prove it, here is my counting method. So, let's begin.

When you count, you probably go one, two, three, four, five, six, seven, eight, nine, ten. When working with real numbers, that works fine. However, this undermines what is actually happening. We use "decimal" counting. This means base 10. But what does that really mean? The base refers to the number of possible values. The values are zero through nine. The number ten is actually not a unique value. It is two values. That is a 1 in the second place and a 0 in the first. Each number you see individually is a value in a specific spot. To show this further, let us take the number 1234. You have a 1 in the fourth place, 2 in the third, 3 in the second, and 4 in the first. Using this same model and some math, we can draw this out to there you get a basic equation where the individual number can be represented relative to the base numbering and its place. It would look like base^place*value. We can then get the actual number by obtaining the sum. So let's expand this further for the number 1234. To do so, we first need to establish what the place value is. The place value starts at 0, because we will take advantage of some special math properties. Thus the number 1234 would be expanded with the base being a constant, in this case 10.

(10^3*1) + (10^2*2) + (10^1*3) + (10^0*4)

The special rule to remember is anything to the power of zero is always one. We have 4 places, starting at zero and ending in 3.

(1000*1) + (100*2) + (10*3) + (1*4)

Hopefully at this point, you can now see the relation of the place with the base. Now we put in the values.

1000 + 200 + 30 + 4

With that we get 1234 as our total value. It may seem super convoluted, but this is the realistic way numbers work. Understanding this, we can use the exact same method to convert any number from any base to a number we humans of base 10 calculations can understand. So let's show the relation to binary, or base 2. Picking an arbitrary binary value, 10011010, let us calculate what that is in a number we can understand. To start out with, we have 8 place values, and a base of two. That should let us drop in all the information nice and easy.

(2^7*1) + (2^6*0) + (2^5*0) + (2^4*1) + (2^3*1) + (2^2*0) + (2^1*1) + (2^0*0)

There are ways to shorten it, which if you can't see you should momentarily, but I will go through all the steps to actually show what it all looks like. So the next step is as follows:

(128 * 1) + (64 * 0) + (32 * 0) + (16 * 1) + (8 * 1) + (4 * 0) + (2 * 1) + (1 * 0)

Since anything times zero would be zero, you could have technically just thrown out those sections from the equation, but we will continue on with them in there.

128 + 0 + 0 + 16 + 8 + 0 + 2 + 0

This then means our binary value was the number 154. Just to show it works for any base number, let's do an octal number. Since it is octal, that means the numbers can be anything with values 0 to 7. Let's try 651.

(8^2*6) + (8^1*5) + (8^0*1)
(64*6) + (8*5) + (1*1)
384 + 40 + 1
425

While the math itself may be hard, getting the equation makes it easy to just figure it out with any old calculator. Working with hexadecimal works a little differently, as we need to do some extra translation. Hexadecimal is base 16, which means the values are 0 to 15. Since we have only ten numbers, 0 to 9, we need to create more numbers. For this, we use letters. The letter values are as follows.
A=10
B=11
C=12
D=13
E=14
F=15

With this extra bit, we can translate a hexadecimal number to an understandable number. Let's use the number 1C3A. The numbers here will be much larger, but as long as we have the equations down, it should be easy.

(16^3*1) + (16^2*C) + (16^1*3) + (16^0*A)

Now, let's substitute any letters and we should be able to roll on through it.
(16^3*1) + (16^2*12) + (16^1*3) + (16^0*10)
(4096*1) + (256*12) + (16*3) + (1 * 10)
4096 + 3072 + 48 + 10
7226

As you can see, we can re-use the same pattern and adapt to anything we need. So long as we keep track of all the information necessary, it's easy enough. So to reiterate, the base is a constant for the whole equation. Each value number is actually two pieces of information, place and value. Common bases for number systems are binary (2), octal (8), decimal (10), and hexadecimal (16). Even so, you can pick any base you want, even lucky 13.

Now, how do we reverse it? The method is a little more obscure, however it is still doable. While the method mentioned before is not sensitive to the order since it is a sum, going backwards is. The good news is, we are worried about the same information of base, place, and value. So let's pick a number we did before, 154, and convert that back to binary. The way we do that is divide by the base and use remainder for the value and the quotient for the next step. We work our way through more or less as (total/base) -> 1 if remainder else 0 -> (quotient/base) ...

A bit more confusing, but let me show how that goes.
154/2 = 77 -> 0

Now the quotient is 77.
77/2 = 38.4 -> 1

The quotient is 38, remainder is 4, so we have a 1.
38/2 = 19 -> 0
19/2 = 9.5 -> 1
9/2 = 4.5 -> 1
4/2 = 2 -> 0
2/2 = 1 -> 0
1/2 = 1.5 -> 1

Now we take all of what is at the end and reverse it to get 10011010, what we started with. Numbers other than binary are a little easier if you know the modulo operator. With an equation, it would be more along the lines of using modulo(%) and integer division for the quotient to look like (value % base) -> (quotient % base)... Making this a little easier with a calculator. However, the structure is the same. So let's use 425 and go back to octal.

425/8 = 53.125 -> 0.125 = 1/8 giving us a modulo 1
53/8 = 6.625 -> 0.625 = 5/8 giving us a modulo 5
6/8 = 0.75 -> 6/8 giving us a modulo of 6

We take that and reverse the order and you get the original we started with in octal, 651. Now let's tackle the one last number, 7224, and get it back to the original hexadecimal. As before it will require the extra step of substitution to get the letters in there. Let's roll through this.

7224/16 = 451.625 -> 0.625 = 10/16 gives us a modulo 10 which is A
451/16 = 28.1875 -> 0.1875 = 3/16 gives us a modulo of 3
26/16 = 1.75 -> 0.75 = 3/4 = 12/16 gives us a modulo 12 which is C
1/16 = 1 gives us a modulo 1

Now we take those values, run them backwards and we get 1C3A. With that, you can hopefully see that converting around different base numbers is actually a simple process and hopefully reveals enough information to truly understand what counting is really representative of.

I also want to talk about some bonus information. Number Order. So the way numbers get processed can be in relation to either the smallest to largest value, which is called little endian, or largest to smallest, called big endian. When you get deep into computer programming or number processing, this becomes very important as to preserving the significant figures we want to be concerned with. As I mentioned before, since converting numbers into decimal was a sum that the order did not matter. That means you can process the numbers in reverse order similar, just like when you convert them off into other bases.

That's all there is to it! With any old calculator you should now be able to do conversions of binary, hex, octal, or whatever else you fancy. Hope it helps someone.

Tag Cloud

.NET (2) A+ (5) ad ds (1) addon (4) Android (4) anonymous functions (1) application (9) arduino (1) artificial intelligence (1) backup (1) bash (6) camera (2) certifications (3) comptia (5) css (2) customize (11) encryption (3) error (13) exploit (5) ftp (1) funny (4) gadget (4) games (3) GUI (5) hardware (16) haskell (6) help (14) HTML (3) imaging (2) irc (1) it (1) java (2) javascript (13) jobs (1) Linux (19) lua (1) Mac (4) malware (1) math (6) msp (1) network (13) perl (2) php (3) plugin (2) powershell (8) privacy (2) programming (24) python (10) radio (2) regex (3) repair (2) security (16) sound (2) speakers (2) ssh (1) story (5) Techs from the Crypt (5) telnet (1) tools (13) troubleshooting (11) tutorial (9) Ubuntu (4) Unix (2) virtualization (2) web design (6) Windows (16) world of warcraft (1) wow (1) wx (1)