Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack (1) Thread Tools Display Modes
Old 17-10-2005, 01:47 PM   1 links from elsewhere to this Post. Click to view. #1 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default Decimal, Binary and Hexadecimal




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,163 84 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:



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.

Code:
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:



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.

Code:
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:

Code:
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

Last edited by rohan; 04-11-2007 at 09:55 PM. Reason: Factual inaccuracies
rohan is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 17-10-2005, 02:03 PM   #2 (permalink)
In The Zone
 
anomit's Avatar
 
Join Date: Mar 2005
Location: Kharagpur
Posts: 252
Default

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!!!!!!!!!!!!
anomit is offline  
Old 17-10-2005, 07:04 PM   #3 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default

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
__________________
If there wasn't greed, we still would have been single-celled organisms.
rohan is offline  
Old 17-10-2005, 08:00 PM   #4 (permalink)
Alpha Geek
 
NikhilVerma's Avatar
 
Join Date: May 2004
Location: India
Posts: 930
Default



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....
NikhilVerma is offline  
Old 18-10-2005, 03:36 PM   #5 (permalink)
Alpha Geek
 
Maverick340's Avatar
 
Join Date: Mar 2004
Posts: 635
Default

This guy is 14 !!!!
One Helluva tut!!!
Lovley1!!!!
__________________
You and Me forever be ...
--
PSpwned
Maverick340 is offline  
Old 18-10-2005, 03:40 PM   #6 (permalink)
deadman
Guest
 
Posts: n/a
Default

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

no surprise if this is made sticky

deadman is here
 
Old 20-10-2005, 12:04 PM   #7 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default

Thanks to everyone for their comments. Additions coming soon.
__________________
If there wasn't greed, we still would have been single-celled organisms.
rohan is offline  
Old 24-11-2005, 10:32 AM   #8 (permalink)
In The Zone
 
::cyborg::'s Avatar
 
Join Date: Oct 2005
Location: NEW DELHI
Posts: 436
Default

excellent rohan do u know something abt assembly language
::cyborg:: is offline  
Old 24-11-2005, 02:33 PM   #9 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default

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.
__________________
If there wasn't greed, we still would have been single-celled organisms.
rohan is offline  
Old 24-11-2005, 07:50 PM   #10 (permalink)
Alpha Geek
 
Join Date: Oct 2005
Location: Thane
Posts: 611
Default

u r reminding me DLDA subject. it was a nightmare to clear DLDA.
But Thanks.
ashnik is offline  
Old 25-11-2005, 07:42 PM   #11 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default

what's DLDA?
__________________
If there wasn't greed, we still would have been single-celled organisms.
rohan is offline  
Old 01-12-2005, 10:22 PM   #12 (permalink)
SNIST Screamer !!
 
vijay_7287's Avatar
 
Join Date: Aug 2005
Location: Hyderabad
Posts: 1,163
Default

u r still in school

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

keep it up !!!!
__________________
http://snistscreamers.blogspot.com/
http://insidevoidmain.blogspot.com/

SNIST-Blog: the Voice of SNIST... http://www.snistblog.com/
vijay_7287 is offline  
Old 01-12-2005, 10:24 PM   #13 (permalink)
Wise Old Owl
 
dIgItaL_BrAt's Avatar
 
Join Date: Jan 2005
Posts: 1,135
Default

gr8 work dude!. Very well done.
__________________
A)bort, R)etry, I)nfluence with large hammer.
dIgItaL_BrAt is offline  
Old 05-12-2005, 06:57 PM   #14 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default

thanks both of u.
but, still ain't getting the response expected
__________________
If there wasn't greed, we still would have been single-celled organisms.
rohan is offline  
Old 05-12-2005, 07:10 PM   #15 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default

Defect Logging and Data Analysis i think... google says so...
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 05-12-2005, 07:17 PM   #16 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default

Really didn't get you...
__________________
If there wasn't greed, we still would have been single-celled organisms.
rohan is offline  
Old 05-12-2005, 07:21 PM   #17 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default

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

sorry if confused you...

BTW, jus completed reading the tut and its AWESOME !
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 05-12-2005, 07:25 PM   #18 (permalink)
In The Zone
 
rohan's Avatar
 
Join Date: Mar 2004
Location: Bangalore
Posts: 297
Default

no prob, but since i asked that a looooong time ago, got confused, thanks anyhow
__________________
If there wasn't greed, we still would have been single-celled organisms.
rohan is offline  
Old 06-12-2005, 12:30 PM   #19 (permalink)
Alpha Geek
 
Join Date: Oct 2005
Location: Thane
Posts: 611
Default

DLDA
Digital Logic Design and Applications
sem III (2nd yr) comp Engg subject, Mumbai University
ashnik is offline  
Old 07-12-2005, 06:40 PM   #20 (permalink)
In The Zone
 
::cyborg::'s Avatar
 
Join Date: Oct 2005
Location: NEW DELHI
Posts: 436
Default

can anyone of u plz give me a good tutorial on assembly and c in a pdf form :roll: plz
::cyborg:: is offline  
Old 10-12-2005, 10:43 PM   #21 (permalink)
In The Zone
 
himtuna's Avatar
 
Join Date: Apr 2005
Location: Delhi
Posts: 241
Default

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.
__________________
http://www.himtuna.com/
http://www.himanshuthakur.com/
Do good to be good !
himtuna is offline  
Old 02-01-2010, 05:05 PM   #22 (permalink)
Right Off the Assembly Line
 
Join Date: Jan 2010
Posts: 1
Default Re: Decimal, Binary and Hexadecimal

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
davitz38 is offline  
Old 02-01-2010, 05:41 PM   #23 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,938
Default Re: Decimal, Binary and Hexadecimal

^^^Lol see the date before posting......!! This is an 5yrs old thread......!!
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES
krishnandu.sarkar is online now  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


LinkBacks (?)
LinkBack to this Thread: http://www.thinkdigit.com/forum/programming/17090-decimal-binary-hexadecimal.html
Posted By For Type Date
All about Unicode /programming with Unicode [tutorial] This thread Refback 06-08-2010 01:08 PM

 
Latest Threads
- by trublu
- by Tenida

Advertisement




All times are GMT +5.5. The time now is 01:15 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2