Home arrow Forums
OSDEV Forums  


anonymous
Visitor

Fresh Osdever
Posts: 0
graphgraph
Karma: 0  
how do i convert Decimal to binary? - 2005/07/06 10:51 how do i convert Decimal to binary?
  | | The administrator has disabled public write access.
OSDEV
Community
Advertisement
   
smiddy
User

Junior Osdever
Posts: 7
graphgraph
Karma: 0  
Re: how do i convert Decimal to binary? - 2005/07/06 11:17 Decimal is based on 10, where 10^y is each digit, where (-Inf < y < Inf). Sticking with integers then, 10^0 is the first digit and is from 0 to 9, the tenth digit moves to 10^1, thus 100 is 10^2 and so on...

Now, binary is similar, where 2^y signifies each binary digit, where (-Inf < y < Inf). Considering the same approach here, using only positive infinity and integers, 2^0 is your first digit. A decimal 2 would be 2^1, thus 2^0 = 0 and 2^1 = 1.

So, to convert 10 to binary, you'd need to know upto 2^3, since 2^4 = 16 and is too many. You have the places or digits 2^3 2^2 2^1 2^0 or four bits. They correspond as 8 4 2 1. In calculating a total you would multiply each digit by its significance and add each digit. For example: (1 x 8) + (0 x 4) + (1 x 2) + (1 x 1) = 10 decimal = 1010 binary.

I hope this clears it up for you and that you can convert your own examples from decimal to binary now.
  | | The administrator has disabled public write access.
anonymous
Visitor

Fresh Osdever
Posts: 0
graphgraph
Karma: 0  
Re: how do i convert Decimal to binary? - 2005/07/07 14:48 Hexadecimal is often used as it is easier to convert from binary to hexadecimal.
To explain it is best to look at some examples:
Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, 10, 11, 12, 13, 14
Binary: 0, 1, 10, 11, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000, 10001, 10010, 10011, 10100
(I apologise if any of those are wrong I just wrote them down in a hurry)

The key thing with the binary and hexadecimal relationships is that you can break up each binary number into 4 digits and those 4 digits can be converted seperatly from the rest of the number. The best way to explain is though example.
Hex: 1
Binary: 0001
Hex: f
Binary: 1111
Hex: 1f (31 decimal)
Binary: 0001 1111 (See how each hex digit corelates to 4 binary digits?)

There are quite a few converter scripts out there (for when you feel to lazy to convert everything yourself) a quick google found:
http://www.tonymarston.net/php-mysql/converter.php

Also some scientific calculator programs (and calculators) will do the conversion by pressing a special builtin button. (best to know how it works rather than depend on a calculator, but if your converting lots of numbers at once it would be silly not to use a converter program)

Also in assembly often the assembler can convert numbers for you. (check the documentation for your compiler but often putting a 'h' or 'b' after a number will inform the assembler what number base you are using).

Hope this helps, say if you need any further help getting your head around different base systems.

Daniel
  | | The administrator has disabled public write access.
deadmutex
User

Junior Osdever
Posts: 7
graphgraph
Karma: 0  
Re: how do i convert Decimal to binary? - 2005/07/07 23:51 One simple way of doing this would be to take the decimal number and divide it by 2. You would then continue to divide the results(as whole numbers) by 2 until you get 0. The remainders from the divisions would result in a binary number from left to right.

Example: 113d is equal to 1110001b

113 / 2 = 56 remainder 1 - 1 LSB
56 / 2 = 28 remainder 0 - 0
28 / 2 = 14 remainder 0 - 0
14 / 2 = 7 remainder 0 - 0
7 / 2 = 3 remainder 1 - 1
3 / 2 = 1 remainder 1 - 1
1 / 2 = 0 remainder 1 - 1
  | | The administrator has disabled public write access.
anonymous
Visitor

Fresh Osdever
Posts: 0
graphgraph
Karma: 0  
Re: how do i convert Decimal to binary? - 2005/07/11 16:32 Thanks
  | | The administrator has disabled public write access.

A WebArticles site. Sponsored by Evoleto. Motorola V525 / Business Directory / Delaware Incorporation / Home Made Bazaar