If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Tutorial: Binary, hex and decimal .. oh my!

After a little discussion with some users in IRC, I was asked on how I teach the conversation from hex to binary to decimal. So I've decided to post everything here along the lines that I usually teach in class.

First, let's look at binary. It is merely a 1 (on) or 0 (off). We see binary used in a variety ways from permissions (in unix) to tcp/ip addresses to attribute settings. (space out the bit so that a decimal appears under each 0)

0 0 0 0 0 0 0 0
128 64 32 16 8 4 2 1

So if my binary was like below:

0 1 1 0 1 1 0 0
128 64 32 16 8 4 2 1

This means that the bits 64, 32, 8, 4 are turned on totalling a value of 108 (64+32+8+4).

If you have to go beyond the 8 bits (the examples above are 8 bits as found in IPv4 addressing aka 32bit addressing --> 4x8bits), just continue doubling the numbers. So if I had to go to 12 bits (aka Fat12) I would have:

0 0 0 0 0 0 0 0 0 0 0 0
2048 1024 512 256 128 64 32 16 8 4 2 1

And so on.

Simple, eh? Now, how about Hex? Well Hex aka Hexadecimal is a "base-16" numbering system. Binary is a "base-2" (based on 2 digits) and decimal is "base-10" (based on 10 digits).

So now I can go from binary to decimal to Hex. This little chart is easy to setup and handy to have. Once you have this the rest is straightforward. An example:

0 1 1 0 1 0 1 0 1 1 1 0
2048 1024 512 256 128 64 32 16 8 4 2 1

Notice that there are 12 bits. Seperate your bits into groups of 4 like so:

0 1 1 0 1 0 1 0 1 1 1 0

And do the hex for each (hex is easier than decimal). This example would come out to:

6 A E

Sometimes written as 6AExh. Now do the decimal:

0 1 1 0 1 0 1 0 1 1 1 0
2048 1024 512 256 128 64 32 16 8 4 2 1

So, 1024+512+128+32+8+4+2 = 1,710(barring any addition errors that should be it). Most of my students get errors in the decimal portion due to addition errors. Give it a try and its best to do it a few times on paper to practise. Once you get that down, it will be straightforward after that.

If you want some more examples, let me know and I will put some more up.

i hope you...

i hope you write tutorials...thanx for the mini-lesson... although i knew this already, i encourage you to make tutorials and such and perhaps start your own website? cuz then, you know, you could shamelessly promote your website here too, like adore from hackers does

If your up to it..

How 'bout mathematical operations in hex and binary? addition, subtraction, multiplication, division, etc. I work on it in my free time, but school takes up most of it and anionline and learning programming the rest.