Number Conversions

(1/1)

Richard Marks:
Number Conversions
as explained by Richard Marks 5-21-2008

As a programmer, numbers should not intimidate you.
I find that many people (myself included) have trouble understanding different number systems
such as hexadecimal and binary. I recently had to deal with these two bases a LOT and learned
a few tricks that I'm going to share with you.

You can easily convert binary to hex as long as you can count.
Let's start simple. The letter A in ASCII has a decimal value of 65.

Lets see what that looks like to the computer eg. in binary.

01000001 .. well thats confusing isn't it? Not really if you understand how to read it.

I'm going to display that vertical to make explaining this easier.

Code:

0 - bit # 7 | 128    off
1 - bit # 6 | 64    on    64 +
0 - bit # 5 | 32    off
0 - bit # 4 | 16    off
0 - bit # 3 | 8        off
0 - bit # 2 | 4        off
0 - bit # 1 | 2        off
1 - bit # 0 | 1        on    1 = 65


Okay? Got it? Its that simple.
Big numbers scare the hell out of a LOT of people. You're about to see
some VERY big numbers. Don't run away!

Now, counting keeps going the more bits your have (that was just 1 byte)
What if you have a 32-bit number?
Well you have to keep on adding up those numbers!

Let's take 4294836225d for instance:

In binary it looks like this:

Code:

11111111 11111110 00000000 00000001
 Byte 3   Byte 2   Byte 1   Byte 0


But what does it mean and how does that mean that BIG number?
I'm going to show you how!
And I promise this is the last vertical binary listing I'll do.

Code:

Byte 3
1 - bit # 7 | 2147483648    on    2147483648 +
1 - bit # 6 | 1073741824    on    1073741824 +
1 - bit # 5 | 536870912        on    536870912 +
1 - bit # 4 | 268435456        on    268435456 +
1 - bit # 3 | 134217728        on    134217728 +
1 - bit # 2 | 67108864        on    67108864 +
1 - bit # 1 | 33554432        on    33554432 +
1 - bit # 0 | 16777216        on    16777216 +

Byte 2
1 - bit # 7 | 8388608        on    8388608 +
1 - bit # 6 | 4194304        on    4194304 +
1 - bit # 5 | 2097152        on    2097152 +
1 - bit # 4 | 1048576        on    1048576 +
1 - bit # 3 | 524288        on    524288 +
1 - bit # 2 | 262144        on    262144 +
1 - bit # 1 | 131072        on    131072 +
0 - bit # 0 | 65536        off

Byte 1
0 - bit # 7 | 32768        off
0 - bit # 6 | 16384        off
0 - bit # 5 | 8192        off
0 - bit # 4 | 4096        off
0 - bit # 3 | 2048        off
0 - bit # 2 | 1024        off
0 - bit # 1 | 512        off
0 - bit # 0 | 256        off

Byte 0
0 - bit # 7 | 128        off
0 - bit # 6 | 64        off
0 - bit # 5 | 32        off
0 - bit # 4 | 16        off
0 - bit # 3 | 8            off
0 - bit # 2 | 4            off
0 - bit # 1 | 2            off
1 - bit # 0 | 1            on    1 = 4294836225


As you can see above, each bit (counting from the right) is exactly double the previous bit.
(I can count to bit #0 of Byte #2 without thinking about it..you should practice this counting.
I call it "my doubles" and I've been doing this since I was 8 years old!)

Okay, so now you know a bit (lame pun) about binary numbers.
What about the cryptic hexadecimal numbers that scare away so many?
Yeah thats easy too.

I'm sure that you have seen hexadecimal numbers somewhere in your programming life.
If not, open a terminal and type in xxd -u /boot/vmlinux > ~/vmlinux.hex and open that
up in a text editor. I like SciTE personally.
Okay, so yeah..thats hex and quite confusing if you're uninformed.

Hex is a base 16 counting system

Code:

Decimal:     0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Hexadecimal: 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F


Let's look at the letter A in decimal again: 65

Now, in hexadecimal what would that be? Well there is an easy way to find out!
Remember the binary for 65? 01000001 ? ok good.
Cut that in half creating two "nibbles" of 4 bits each:

    left (low) nibble 0100 0001 right (high) nibble
 
    Forget about the high nibble for now.
   
    Now you only have 4 bits to worry about.
   
    0100
   
    that in itself is what? ... (look at the first vertical byte list if you forgot)

    got it yet? I did. ....  4h!
   
    Now what about the high nibble? 0001 .... easy. its 1h
   
    put them together, 41h ... that is the hexadecimal equivalent of 65 in decimal!
   
So, when ever you need to convert a decimal number to hex, its easiest to first
get the binary of it, then bust that into nibbles. :)

As a final example, lets look at a stream of binary bits of no origin than my head.

101010101100010111100010101010010101001010100101

yikes... okay.. we can do this...easy.

just break that into nibbles

1010 1010 1100 0101 1110 0010 1010 1001 0101 0010 1010 0101

and now lets convert each nibble into hexadecimal

A A C 5 E 2 A 9 5 2 A 5  that wasn't hard.

Okay, thats it for today..want to know more? Ask me.

RedSlash:
It is good to know that scientific calculators support an easy way of converting between numbers of different bases. These are DEC, HEX, BIN and OCT buttons on your calculator. If you don't own one, your OS's built in calculator software in scientific mode has these buttons.

As an example, to convert decimal 189 to binary, first press DEC to put your calculator in decimal mode. Then
type '189', then press BIN. Voila!

Richard Marks:
Quote from: RedSlash on July 13, 2009, 07:06:53 PM

It is good to know that scientific calculators support an easy way of converting between numbers of different bases. These are DEC, HEX, BIN and OCT buttons on your calculator. If you don't own one, your OS's built in calculator software in scientific mode has these buttons.

As an example, to convert decimal 189 to binary, first press DEC to put your calculator in decimal mode. Then
type '189', then press BIN. Voila!



Well yeah there ARE easy ways to do the conversions, however that doesn't TEACH anything.
Anyone who wants to do any low-level coding needs to have a solid understanding of the way that the numbers work.
Which is why I wrote the article. :)

Also, for the record, you can type 189 in binary in google and get your conversion as well.
Lots others like 64 in hexadecimal and 0xDEADBEEF in decimal will work too.

:)

RedSlash:
Quote

Also, for the record, you can type 189 in binary in google and get your conversion as well.
Forgot about google! See.. you TAUGHT me something!

Richard Marks:
Quote from: RedSlash on July 13, 2009, 08:20:32 PM

Quote

Also, for the record, you can type 189 in binary in google and get your conversion as well.
Forgot about google! See.. you TAUGHT me something!

LOL

Navigation

[0] Message Index