Binary and hexadecimal written by
Roticv
Binary and hexadecimal is are both different but yet similar number system which are
*extremely* important to any programmer. Binary is base 2 number system, while hexadecimal
is base 16 number system.
The reason for saying why binary is important to a programmer:- In the electronic world,
the only practical means to store data is to is the way of on and off (There is no way to
tell whether how high is the voltage or how low it is). Therefore in that sense binary is
evolved. Data is stored in on or off (1 or 0), so it means that learning binary is
learning machine code. (As some people says, "Real man code in binary") Oh yes,
there is 10 types of people in the world, one who can read binary and one who cannot.
The reason for saying why hexadecimal is important to a programmer:- It makes no sense to
type in 1s and 0s, thus the hexadecimal is evolved. In the sense, it is more practical to
adopt a base 16 number system than to use a base 10 number system. By the way, hexadecimal
was used to be called sexadecimal, but due to some reasons the people at IBM decided to
call it hexadecimal instead. (Some programmers says "Real man codes in hex".
Well I do know how to code in hex :p)
Binary -> Decimal
Think of reading binary as reading normal number, but in the sense, the numbers mean
something else. The last digit means is the to the power 2^0, the second last digit is to
the power 2^1 and some one and so forth.
Example:
0000b = 0
0001b = 1
0010b = 2
0011b = 3
0100b = 4
0101b = 5
0110b = 6
0111b = 7
1000b = 8
1001b = 9
1010b = 10
11011010b = 1*2^7 + 1*2^6 + 1*2^4 + 1*2^3 + 1*2^1
= 128 + 64 + 16 + 8 + 2
= 218
01010111b = 1*2^6 + 1*2^4 + 1*2^2 + 1*2^1 + 1*2^0
= 64 + 16 + 4 + 2 + 1
= 87
(*Note: the b which ends every binary number is used to inform people that the number is
in binary, it is just a notation.)
Hexadecimal -> Decimal
Till this point you should be able to understand binary, and since you understand how base
2, you should be able to understand how does hexadecimal works. The conversion of
hexadecimal to decimal is almost similar to the conversion of binary to decimal. Well,
applying the same concept:-
Example:
01h = 1
02h = 2
03h = 3
04h = 4
05h = 5
06h = 6
07h = 7
08h = 8
09h = 9
0Ah = 10
0Bh = 11
0Ch = 12
0Dh = 13
0Eh = 14
0Fh = 15
10h = 16
F4h = 15*16^1 + 4*16^0
= 240 + 4
= 244
F34Ah = 15*16^3 + 3*16^2 + 4*16^1 + A*16^0
= 15*4096 + 3*256 + 4*16 + 10
= 61440 + 768 + 64 + 10
= 62282
(*Note: the h which ends every hexadeciaml number is used to inform people that the number
is in hexadecimal, it is just a notation. Some HLL programmer would prefer hexadeciaml to
be in prefixed with 0x, but I prefer it to end with h.)
Binary -> Hexadecimal
Now is one of the most important section of the this tutorial. What is the point of
knowing binary and hexadecimal exist when you can convert from one to the other?
Example:
0111 1010b
| |
7h - - Ah
Therefore 01111010b -> 7Ah
0100 0111b
| |
4h - - 7h
Therefore 01000111b -> 47h
A full online book of updated information can be found here.