# Decimal, Binary and Hexadecimal



## rohan (Oct 17, 2005)

*img391.imageshack.us/img391/2167/decbinhexheader6df.gif

*Prologue*: Intoduction

Well, I've been learning the Assembly Language(i'm a quite a n00b to it) and it requires helluva knowledge of binary and hexadecimal. Luckily, I knew the basics earlier(thank god). But, I've seen on many forums that people really get stuck at Binary and Hexadecimal system and especially at converting values from one system to another. Moreoves, there are no exact and good tutorials on the net for this. Here is a small tutorial from my side to make things easier:

*Chapter 1. Number Systems*

Man has always tried to find out ways to count. Numbers have been an integeral part of the evolution of mankind. The Early Man used to count using sticks. | meant 1, || meant 2 and so on. This was succesful because the applications of these numbers for him was quite small. But, when his operations with those numbers increased, he needed to find a better way. Historians believe that this gave rise to the Roman Numeric System. Here, ||||| (which meant 5) was replaced by V and ||||||||| (which meant 10) was replaced by X.

Then came a major breakthrough with the advent of the Decimal number system. The decimal number system was compact and was the first numeric system in which numerals were an entity i.e they had a fixed place value in a given number and it had a base.

The biggest invention or addition to the decimal system was said to be the value of nothing i.e 0 (the world IS a funny place)...

*1.1)Base*

What is a base? {Soruce: Oxford Dictionary} : _Mathematics_ a number used as the basis of a numeration scale.

A base is the maximum number of digits in a numeral system(it's a coarse definition). Decimal has a base 10( possible digits: 0,1,2,3,4,5,6,7,8,9) whereas Binary has a base 2(possible digits: 0,1) and Hexadecimal has a base 16(possible digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). Now, let me explain the oxford decitionary's meaning.

Historians beleived that the origin of the base 10 for decimal lies in the fact that humans have 10 fingers and fingers is the most primitive tool for counting.

Consider the number 432. What are the place values of 4,3 and 2? They are increasing in powers of 10 i.e 100,10 and 1. Hence the base is the number which is used as the basis of a numeration scale.

*1.2)Binary Basics*

Binary numbers are made up of 0 and 1. The digits of a binary system are called 'bits'. The bit with value as '1' is said to be ON, whereas the bit with value '0' is said to be OFF. 0010 is a binary number. Remember that 0010 is equal to 10. Then why do we still put the two zeroes on front of it? It is because, although the number uses only 2 bits, but when it is stored in the CPU registers, it occupies the complete space of 4 bits. 4 bits collectively are called as a 'nibble' and two nibbles i.e 8 bits make up a 'byte'. Two 'bytes' make up a 'word' and two words make up a 'double word' (Yes, same as dword, which is a frequently used term in the Windows registry). Double word, however is rarely used in low level languages.

*1.3)Place Values*

How to define a decimal number in the long form? Let's define 432. It can be defined as:

     4x10Â² + 3x10 + 2x1

or, you can also say as:

     numeral x base^[place value]   (Here, ^ means 'to the power of')

Now, coming to binary. Decimal system has place values increasing in powers of it's base i.e 10. Same here in binary system. You must be knowing that binary system has two possible values: 0 and 1. The place values are in increasing powers of 2. Therefore the place values are 1,2,4,6,8,16,32,64,128,256,1024,2048,4096,8192,16384 and so on... Check this number:

10101101b                       (by convention binary numbers are suffixed by 'b')

Let us define it in the way we defined a decimal number(numeral x base^[place value]):

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

So it would be something like:

1*128 + 0*64 + 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 1*1
which is: 173

So, 173 in binary is 10101101b. Easy, aint it? Let us get the values of number 1 to 10:

*img359.imageshack.us/img359/561/dec2bin5hu.gif

*1.4) Hexadecimal*

Hexadecimal was made for easier denotation of Binary values. You can see that the 'bit' is the building block of all binary numbers. It has two possible values, 1 and 0. So, denoting a bit is not at all complex, and is really not needed because if we denote bits, we would be anyway saying the complete binary number. The next, higher building block is a 'nibble'. And hexadecimal denotes every nibble. Hexadecimal has a base 16. You will learn a lot more about Hexadecimal in Chapter 2: Conversions, ahead in this tutorial. Here are few things you need to know about a hexadecimal:

:.: A digit in Hexadecimal also has a place value, which is it's face value multiplied by 16 to the power of the current place. For example in '34A6', 'A' has the place value (10) x (16)^1, which is 160. [A in hex = 10 in decimal]. 
:.: Hexadecimal can be seen as an easy representation of the binary numeral system. For ex., a whole byte can be written in two characters.
:.: By convention, hexadecimals have a suffix 'h' and more commonly, have a prefix '0x'.

*Chapter 2. Conversions*

It is very necessary for a low level developer(I mean who develops programs in low level programming language loke Assembly Language, not the one who is a programmer of low grade) to know how to convert values between decimal, hexadecimal and binary. It is very easy indeed to convert these values. You will never when you will start speaking binary!!!

*2.1) Conversion Basic*

Basically, to convert a number from a decimal system to any other proper system, you divide the number by the base and note down the remainder. Then you divide the quotient with the base again and get the remainder and so on until the quotient is zero. Then all the remainders are reversed in their orders and you get the numbers in another numeric system.

*2.2) Converting Decimal to Binary and vice versa*

There are two ways of converting a decimal number to binary:

2.2.1) Just break up the numbers into powers of 2. It's very tough, but it's quite easy for small numbers. For ex. to convert 20 to binary, just think of a way in which the place values of the bits would add up to 20. It can be found that 16 and 4 add up to 20. 4 is the 3rd bit and 16 is the 5th bit. So binary value would be 10100. Here, 5th bit is ON because it is added to get the desired number and also is the 3rd bit,because it is also used to add to get the desired number.

2.2.2) The scientific approach: Let us convert 20 to binary.


```
20/2 | Quotient: 10  Remainder: 0
10/2 | Quotient: 5   Remainder: 0
5/2  | Quotient: 2   Remainder: 1
2/2  | Quotient: 1   Remainder: 0
1/2  | Quotient: 0   Remainder: 1
```

If you see in the above examples, first we have divide the number by 2(the base of binary system) and noted the quotient and remainder. The quotient is again divided by 2 and the remainder is noted. This goes on until the quotient is 0. The remainders we got here are: 0,0,1,0,1. Let us take them in the reverse order: 10100. That's it. That's the binary for 20.

It is upto you to decide which method to use. You can use any method you like, but as I a said it's upto you.

2.2.3) Converting Binary to Decimal: It is extremely easy. Just add the place values of all bits that are ON. Check 1.3 for an example.

*2.3)Coverting Decimal to Binary*

The scientific method is not applicable here because hexadecimal numbers are rather denoters. Binary values are called 'something' in Hexadecimal, so we need to know what are they called. If you see, the building block of all binary numbers is the 'bit'. There is no need to denote a binary, it will complicate matters rather than simplify it. If we go somewhat higher, the building block of binary numbers is the 'nibble'. There are 16 possible values in a nibble. All values have a hexadecimal equivalent. Those values are given in the table below:

*img359.imageshack.us/img359/955/hex2dec2bin0ij.gif

Converting a nibble to binary is easy, but what about converting bytes to nibble? A byte, if you see is made up of two nibbles. Consider a binary number: 10110110b. It is made up of two nibbles: '1011' and '0110'. The hex value for 1011 is B and for 0110, it is 6. So, the hexadecimal value for 10110110b is B6h. The values of constituent nibbles are appended as they appear in the binary number.

Vice versa is as simple as that. Consider B6h. It is made up of 'B' and '6'. B is 1011 and 6 is 0110. So binary would be 10110110b. As simple as that!

*2.4)Converting Decimal to Hexadecimal*

There are again two ways to do that.

2.4.1) Firstly, convert it into binary and then into hexadecimal. This is a very fast method and is mostly used as division by 2 is much easier than division by 16. Converting binary into hexadecimal is no problem at all... as you have seen earlier.

2.4.2) Although the first method is easier, it is always better to know the scientific approach. Here, we know that the base is 16. Let us convert 20 to hexadecimal.


```
20/16 | Quotient: 1 Remainder: 4
1/16  | Quotient: 0 Remainder: 1
```

Here also we divided it by 16 until we got the quotient as as 0. The remainder's obtained were 4 and 1. Let us reverse them. The hexadecimal value we get is: 14h. 

_Vefification_:Let us verify the results obtained.
Now, 14 is made up of two parts: 1 and 4. 1 in binary is 0001b and 4 in binary is 0100b.So, 14 would be 00010100b. You can check it yourself. As you can see, the 5th bit i.e the bit the place value 16, is ON and so is the third bit which has the place value 4. Adding these values gives us 20. Hence this anser is verified.This was a very small example. Let us  consider a bigger number, a much larger number, say: 4367:


```
4367/16 | Quotient: 272 Remainder: 15
272/16   | Quotient: 17  Remainder: 0
17/16    | Quotient: 1   Remainder: 1
1/16     | Quotient: 0   Remainder: 1
```

So, we've got the remainders. Pay attention on the remainder '15'. Can we write it as '15' in a hexadecimal number? No, we cannot. Let us see why. 15 is equal to 1111b. But, 15 in hexadecimal is made up of 1 and 5. 1 is 0001 and 5 is 0110. So, the number would be 00010110b which is actually 22. So, you see we can't use this. Take a look at the table given in 2.3. 15 is denoted by 'F'. The remainders here are 15,0,1 and 1. Reverse them and we get 110F (15 is denoted by 'F'). So hexadecimal of 4367 is 110Fh.

Simple, aint' it?

*Last Word*

|- If you notice, an odd number in binary always has the bit at one's place 'ON'.
|- When dividing any number by 2, the remainder can never be greater than 1, nor smaller than 0(for absolute values of numbers)
|- For converting numbers, better practice a few such conversions. To check and verify your solutions, use the calculator available in all Operating Systems in the scientific mode. You would need a calculator that can divide and return the quotient as well as the remainder. I've made a small program in C++ that does just that. You can download it here: qrdivisor.exe


Comments are highly appreciated. Please post your comments. Thanks for showing your interest in this tutorial. Questions, queries and doubts are welcome.

ThanX

Tools used: ImageShack(For web hosting) | MS Frontpage(For making the tables) | MS Paint(For preparing the tables in image format) | Acrobat Proffesional 7.0(For converting in .PDF format) | thinkdigit forums(For tutorial hosting) | Yahoo! Geocities(For file hosting)

Â©Rohan Prabhu Â©Tritium Skinz Â©Akshar Tutorials


----------



## anomit (Oct 17, 2005)

This has to be the most professional tutorial I have ever seen on any kinda forum. In your next tutorial, maybe you could delve a bit deeper into the Hex and Bin systems and how to perform simple logic operations. 

Keep up the good work man!!!!!!!!!!!!


----------



## rohan (Oct 17, 2005)

Thanks a lot d00d. I have really tried to make it as proffesional as i could. Upcoming additions(If i get some replies to this post):

1.Operations at binary level
2.Graphics at binary level


----------



## NikhilVerma (Oct 17, 2005)

have to say ! That's one hell of a tutorial over there !!!!
Keep it up man !

And really I too feel that it's really professional....


----------



## Maverick340 (Oct 18, 2005)

This guy is 14 !!!!
One Helluva tut!!!
Lovley1!!!!


----------



## deadman (Oct 18, 2005)

This is the most prof. and technical tut i've seen in DIGIT forum

no surprise if this is made sticky

*deadman* _is here_


----------



## rohan (Oct 20, 2005)

Thanks to everyone for their comments. Additions coming soon.


----------



## ::cyborg:: (Nov 24, 2005)

excellent rohan do u know something abt assembly language


----------



## rohan (Nov 24, 2005)

Well, yes. I have just started learning assembly language. I am in 10th so it's a bit difficult learning it, but i can make programs like adding two inputted numbers, making boot programs and that stuff, nothing much above that.


----------



## ashnik (Nov 24, 2005)

u r reminding me DLDA subject. it was a nightmare to clear DLDA.
But Thanks.


----------



## rohan (Nov 25, 2005)

what's DLDA?


----------



## vijay_7287 (Dec 1, 2005)

u r still in school

well dude ur tutorial was better than the stuff they teach in our college

keep it up !!!!


----------



## dIgItaL_BrAt (Dec 1, 2005)

gr8 work dude!. Very well done.


----------



## rohan (Dec 5, 2005)

thanks both of u.
but, still ain't getting the response expected


----------



## QwertyManiac (Dec 5, 2005)

Defect Logging and Data Analysis i think... google says so...


----------



## rohan (Dec 5, 2005)

Really didn't get you...


----------



## QwertyManiac (Dec 5, 2005)

U asked wat was DLDA and so the answer
*doi.ieeecomputersociety.org/10.1109/52.965803

sorry if confused you...

BTW, jus completed reading the tut and its AWESOME !


----------



## rohan (Dec 5, 2005)

no prob, but since i asked that a looooong time ago, got confused, thanks anyhow


----------



## ashnik (Dec 6, 2005)

DLDA
Digital Logic Design and Applications
sem III (2nd yr) comp Engg subject, Mumbai University


----------



## ::cyborg:: (Dec 7, 2005)

can anyone of u plz give me a good tutorial on assembly and c in a pdf form   :roll: plz


----------



## himtuna (Dec 10, 2005)

Dude what to say about you!!!
Just in 10 class!!!
And came out with such a marvelous work.
But remeber next year is not gonna be the same 
You'ill be in 11
Better start making your mind to devote proper time to studys and computer
As working hard on comp would not let you grasp more the 100  marks in computer science.
Better consider this as a warning if taking science stream.
You'ill end getting nothing. 
No good marks means
No full enjoyment of great comp knowledge
A company wouldn't hire you even if you know several languages
It is only experience and good degree after 10+2 that counts.

Well I am in 11 class 
So telling you what I've experienced.
I'm not discouraging to stop loving the comp. but better be aware of real world.


----------



## davitz38 (Jan 2, 2010)

Hi guys,
To convert hex, binary and dec online, I use this string function online tools
Try it, check the hex to binary converter 
Pretty useful!!!!
David


----------



## krishnandu.sarkar (Jan 2, 2010)

^^^Lol see the date before posting......!! This is an 5yrs old thread......!!


----------

