Friday, June 3, 2011

Numbers: Decimal, Binary, Hexadecimal

Numbers with Epic?
(but you can call me Jacob)

Numbers, you love 'em, right? Oh I see, you're not a nerd, well good thing I'm here to explain this to you.
Now, I know what you're thinking, "pointless thread is pointless" - but don't jump to conclusions.
What I'm going to be talking about is the basics of the decimal, binary, and hexadecimal numbers, this may seem foreign to you, so I'm going to start with the complete basics and work our way up to at least a fundamental understanding. I know that may seem pointless to you, but since there's a decent chance you're interested in learning about computers, this might not be so pointless.

For those of you who are trying to be "hackers" or even "programmers" this is a wonderful place to start. Also for those of you trying to reverse engineer in PPC, another good place to start.

I posted this in the hardware section because I talk about Machine Code, and binary. Even PowerPC (which is used on Xbox 360, PlayStation 3, Wii, and old Macs) is very hardware based. If you feel it should be moved, please leave it up to a moderator, they will move it. Thanks!



Machine Code

The first concept we'll have to understand is that of Machine Code. Machine code is the code your computer actually runs. It does not understand any language, not English, Spanish, French, Japanese, Chinese, or any other spoken language. It also doesn't understand C, C++, or C#, or whatever language you know. It solely understands binary. Sure, you can think of binary as a language, but it only has two characters.

Binary is comprised of ones and noughts (BASICALLY: Ones and Zeros), the ones and zeros that represent these ones and noughts are the "two characters" I mentioned earlier. Back in the beginning of the time of computers, everything was written in binary machine code. Nowadays we no longer write in binary (thank god!), we usually use assemblers or compilers that can transform our code (like C) into machine code (binary) for the computer to use.

Fun Fact! Binary's ones and noughts are actually represented to the computer through electrical signals. A one would represent an "on" signal (a strong signal) whereas a zero/nought would represent an "off" signal (although in most cases not completely off, just reduced strength) and these are indeed transmitted through electrical currents.

Here's something you need to understand, binary is written in series of ones and noughts to form an instruction. These instructions are called "words". A word is made up of 32-digits (of binary), or bits. So you could think of one word being equal to 32-bits. The pattern of the binary in the word tells the processor how to execute (or run) the word/instruction. The programs we see today are made of many words strung together.

Now, I just whipped a word at you: processor. I won't go far in depth, but the goal of the processor is to take in data (input) and perform some sort of processing on said data, then store that data in a useful way, thus giving you the output. So basically, user/programmer enters data input, processor executes or processes the data, processor stores the data output in a way useful to the user/programmer. Something to understand is that processors need temporary storage within itself called registers. A language called PowerPC (often abbreviated to PPC) is used in the Xbox 360, PlayStation 3, Wii, and old Macs. I don't know how much you know, but ever since the "hacked" version of the xbox called a "jtag" was banned from being online, people have been trying to create a new method of coming online using PPC. PowerPC has 2 types of registers: integers (defined as r0 to r31) and floating points (defined as f0 to f31), floating points are one of the most powerful aspects of PPC. But let's get back on topic...

What does digital mean? Well, digital means numerical (the root of that word is number). Now, that idea isn't something vitally important right now, but keep that in mind. Now we can't just input a decimal number to a computer and expect something to work, just as the instructions executed by a computer are stored in binary, so are actual numbers. 



Decimal/Decimary Numbers
BUT WAIT! What's a decimal number? Do you mean like 1.2? Nope, the "." is a decimal point, a decimal number is comprised of 10 (deci) possible digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 (0 to 9). This is what I call the "normal" numeric system, because this is what we usually see. We all can recognize 10 as ten, even though in binary 10 (pronounced "one zero" not "ten") is a 2 (if you were thinking in decimal to binary you'd say 2 = 10).

So... how do we actually take these "normal" decimal (the system is actually called denary, so learn the denary system, some call this the "human denary system") numbers and put them into binary? Well, good question!

Lets start by looking at the denary system we all know and love. We start counting at zero (0) and we can easily count up to ten, eventually we reach 11 (eleven), which is 10 plus 1, and then we reach 101, meaning one hundred, no tens, and one unit of one. If we use the term multipliers to describe tens, hundreds, and thousands, then any number can be expressed in terms of its multipliers. The decimary number 1234 could be expressed as:
1 times 10^3 (one multiplied by ten to the third power)
2 times 10^2
3 times 10 ^1
4
That would basically describe the number 1234 in the decimary system.
Please remember, that the carrot symbol (^) is to denote "to the power of".

Did I lose you yet? Well, I hope not. Lets break down what I just showed you (the 1234 example), lets just look at the thousands place. The thousands place is a 1 in the decimary system (1234). Since its in the thousands place, that one does not represent an actual one, but a one-thousand (1000). 10 to the third power (10 times 10 times 10) is equal to 1000. 1 multiplied by 1000 is equal to 1000, thus making that make sense:
1 (times) 10^3 is equal to 1000, which is the value of the 1 in the thousands place.

C'mon, you better not be confused, we're talking normal numbers here!



Binary

Now, let's switch over to binary (oh no!). Don't be concerned its actually quite simple.

Remember our fun fact? Binary is used by computers at the basic level of machine code, it is represented through ones and noughts (ones and zeros). These are actually electrical signals sent to the processor, the one is an on and the zero is an off!

Computers use binary, not denary, thus making it important to understand the fundamentals of binary. In binary numbers are expressed as powers of 2 instead of 10. In denary multipliers go 1, 10, 100, 1000, 10000 and so on, whereas in binary the multipliers go 1, 2, 4, 8, 16, 32, 64, 128, 256 and so on.

Let me start by saying that it can be helpful to remember some of the multipliers, at least the first few. I'm not saying you have to memorize them, but if you're serious, consider it, here's some to learn:

1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536

Alright, lets start by trying to express the decimary number 13 (thirteen) in binary.
Break it down into powers of two.
13 is one 8, one 4, and one 1.

Look at this chart:
8 4 2 1
1 1 0 1

Thus 13 is 1101 in binary. Confused? Don't worry, that example could be kinda rough, lets take a minute to talk about some vocab, and then move on to another example.

Binary digits (1 and 0) are termed "bits", its a short form of "binary digit". So instead of saying binary digit, just say bit. Sometimes in a word (remember that vocab term?) you may need to locate a group of bits. Bits are numbered from zero upwards, the bit zero is the rightmost bit. For example:
76543210 - 7 would be the first bit in that string, and 0 would be the last bit, so you could call that first bit "Bit 7" and the last bit "Bit 0".

Back on topic...

Lets convert to binary! Yay!

Convert the decimary number 255 to binary. Don't look below at the work/answer, think about it, try it yourself, then take a peak below at my answer.

Start with the multiplier above 255, which happens to be 256. 256 is to big (to great) of a number, so try 128.
255 divided by 128=1 remainder 127. So we have a 128.
127 divided by 64 =1 remainder 63.
63 divided by 32 = 1 remainder 31
31 divided by 16 = 1 remainder 15
15 divided by 8 = 1 remainder 7
7 divided by 4 = 1 remainder 3
3 divided by 2 = 1 remainder 1
1 divided by 1 = 1 remainder 0

Therefore 255 in binary is 11111111 (8 ones). You can count it up:
1. 255 divided by 128=1 remainder 127. So we have a 128.
2. 127 divided by 64 =1 remainder 63.
3. 63 divided by 32 = 1 remainder 31
4. 31 divided by 16 = 1 remainder 15
5. 15 divided by 8 = 1 remainder 7
6. 7 divided by 4 = 1 remainder 3
7. 3 divided by 2 = 1 remainder 1
8. 1 divided by 1 = 1 remainder 0

Let's do another example if you're still confused. Let's do the decimary number 471. The multiplier 512 is a bit too big, so lets do 256.
Here's the math:
471 divided by 256 = 1 remainder 215
215 divided by 128 = 1 remainder 87
83 divided by 64 = 1 remainder 23
23 divided by 32 = 0 (because 32 doesn't divide into 23 even once. i.e. it won't "go")
23 divided by 16 = 1 remainder 9
7 divided by 8 = 0 (because it won't go)
7 divided by 4 = 1 remainder 3
3 divided by 2 = 1 remainder 1
1 divided by 1 = 1

So we see that 471 when put into binary is 111010111. That is nine bits.

Basically that means:
1*1 + 1*2 + 1*4 + 1*16 + 1*64 + 1*128 + 1*256 = 471 (the asterisk "*" represents multiplication)
OR
1 + 2 + 4 + 16 + 64 + 128 + 256 = 471

Here are a few more examples:
128 64 32 16 8 4 2 1
25 = 0 0 0 1 1 0 0 1
129= 1 0 0 0 0 0 0 1
56 = 0 0 1 1 1 0 0 0
90 = 0 1 0 1 1 0 1 0

If you don't get/want to use that chart, its okay, just make sure you understand how to convert between decimary and binary.

Hexadecimal

Okay, if you looked at the title of this thread before clicking on it you would have noticed "Decimal, Binary, Hexadecimal". Well, we covered decimal and binary, what about hexadecimal? Well, lets jump right in...

Hexadecimal is commonly seen when reverse engineering programs, and especially in PPC (PowerPC, which I discussed earlier), therefore its important to discuss the basics. You'll also witness the hexadecimal system in chip-programming/logic (if you were to ever create a "rapid fire" chip mod for your controller to a gaming console).

Hexadecimal is usually referred to as "hex" (ever heard of hex codes?). Hex is a numerical system, just like decimal or binary. Anyone remember from elementary school what hex refers to? Well a hexagon has six sides, so hex must mean six!


In the decimary system we had a base of 10 (the digits 0-9), in binary we had a base of 2 (digits 0-1), and in hex we have a base of 16. Just something to point out: deci means ten, bi means two, and hex means six. 
So hold up. We have this little issue here, hex has a base of 16, but "hex" only refers to "six". Well you're forgetting the full name for hex is actually hexadecimal, hex menaing six, and deci meaning ten. 6 + 10 = 16.

Now I know what you're thinking:
When you say a "base of 10" you use 10 different single digits, when you say "base of 16" you must mean that you use 16 different single digits, but there are only 10 different single digits! Therefore, hex is clearly impossible!

Slow down there cowboy. Hex extends to 15 (not 16 because we're including 0) by using letters (specifically the letters A to F).

If you can understand binary, you won't have to worry much about hex. SOMETHING YOU NEED TO KNOW - or the next part won't make any sense: Each hex digit represents 4 bits (also called a nibble).

Here's an example:

255 in binary is 11111111 (8 ones - we already did this, remember?)

To get 255 in hex, first convert to binary, then split up into nibbles (4 bits):
1111 1111
(4 ones) (4 ones)

Each hex digit is a niblle, so 1111 is binary for 15 in decimal, or F in hexadecimal. To show this hex number in code we usually precede it with a dollar sign ($) or the C language standard of "0x". So if you were in the C language, you might say "0xFF" for the number 255.

Lets convert FACE to decimal - 

F 1111
A 1010
C 1100
E 1110

Go on, convert! 

Answer: 64206.

So by now you should understand binary, decimal, and hexadecimal. Enjoy!

If you want more number systems, check out this wikipedia page: http://en.wikipedia.org/wiki/List_of_numeral_systems

There's actually quite a few very neat numerical systems if you want to check them out. Many of them are very job-specific.

So, Why did you read this whole thing?
Well aside from the fact that you probably had a lot of time to kill, you now have a deeper understanding of numerical systems, and how they work, as well as how machines use them.

1 comment:

  1. Your Affiliate Profit Machine is waiting -

    Plus, making profit with it is as easy as 1, 2, 3!

    This is how it all works...

    STEP 1. Input into the system which affiliate products you intend to promote
    STEP 2. Add some PUSH button traffic (it LITERALLY takes 2 minutes)
    STEP 3. Watch the affiliate system explode your list and upsell your affiliate products all by itself!

    Are you ready??

    Your MONEY MAKING affiliate solution is RIGHT HERE

    ReplyDelete