Monday, December 12, 2011

How do we convert a number/value to binary (base-2) ?

For example if we have the value 26. When converted to binary 26 = 11010.





I know the answer but I don't know how to covert from values/numbers to binary.|||Note: You may want to do something different if you are dealing with large numbers. This is just for smaller numbers. Do the division method (what others have posted) if working with relatively large numbers.





Going from Decimal to Binary is just as easy (in my opinion) as going from Binary to Decimal.





B to D





001011





Notice that there are 6 digits total in this Base 2#.





Starting from the right, the rightmost 1 is in the 2^0 place. (1)


The 1 beside the rightmost 1 is in the 2^1 place. (2)


The 0 to the right of that 1 is in the 2^2 place. (4)





Multiply the 1's %26amp; 0's to their place values like this:





2^0 * 1 = 1


2^1 * 1 = 2


2^2 * 0 = 0


2^3 * 1 = 8


2^4 * 0 = 0


2^5 * 0 = 0





THEN, add up the solutions:





1 + 2 + 8 = 11





And THIS number is the answer. So the base 2 number 001011 is 11 in decimal.





Decimal to Binary (going backware)


Just do the reverse process...





2^x = 26





Ok so now you wonder, 2 raised to what power will get me closer to the decimal value 11.





Begin writing down place values (doesn't matter how many)


_ _ _ _ _ _





Ok so now you ask yourself, what is the highest number than I can reach without going over 11.





2^5 ? Hmm...nope. Guess again. 2^5 = 32, and thats over.


2^3 ? Well thats 8, and 2^4 is 16...so 2^3 it is. Mark a 1 in this place holder:





_ _ 1 _ _ _








Ok so you got 8. You have 3 leftover. Now you ask yourself 3 raised to what power will give me 3 or less. One thing to keep in mind is that 2 raised to any power will not give you an odd number. So it is impossible to get a 3 when raising 2 to a power. 2^1 happens to be 2 and that place is to the left of the rightmost position.


Leaving you with this:





_ _ 1 _ 1 _





That leaves us with a decimal value of 1 left over. It so happens that that rightmost place holder is 2^0...which equals one. So there you have it!





_ _ 1 _ 1 1





Now just fill in the rest of the blanks with 0's because we did not need those for the decimal number 11.





0 0 1 0 1 1





So decimal value 11 equals 001011 in base-2. You know, those leftmost 0's are not significant so you can just leave them off, leaving you with just 1011. Just to simplify things :)











Note: If you are ever in doubt about converting between base 2 and decimal, you can always use the calculator that comes with Microsoft XP or Vista. Just go to View...Scientific and you will see options for Binary as well as other bases (hex, oct, etc.)





Hope this helps!|||while(i) {


printf("%d",i%2);


i=i/10;


}|||http://www.mathbits.net/MathBits/CompSci鈥?/a>|||for example 200 in decimal





is there any 128 in 200 ? YES


BINARY : 1


200-128 = 72





is there any 64 in 72 ? YES


BINARY : 11


72-64 = 8





is there any 32 in 8 ? NO


BINARY : 110





is there any 16 in 8 ? NO


BINARY : 1100





is there any 8 in 8 ? YES


BINARY : 11001


8-8 = 0





is there any 4 in 0 ? NO


BINARY : 110010





is there any 2 in 0 ? NO


BINARY : 1100100





is there any 1 in 0 ? NO


BINARY : 11001000








200 in decimal = 11001000 in binary





binary should be 8 bit long.


starting from 128 , 64 , 32 , 16, 8 , 4 , 2, 1


i mean power of 2|||easy way is to use your calculator.


other way you now this


1 ciffer is 1


2 ciffer is 2


3 ciffer is 4


4 ciffer is 8


5 ciffer is 16, 32, 64, 124


and so on, then you start retracting the number


ex 146 - 124 = 22 left = 1


22 is less then 64 and 32 = 00


22-16= 4 left =1


4 is less then 8 = 0


4-4=0 =1


0 is less then 2 and 1 = 00





this gives the binarry number 10010100 = 146|||Doing it by hand:





Keep dividing the number by 2





i.e. for decimal number 26:





26/2 = 13 r 0


13/2 = 6 r 1


6/2 = 3 r 0


3/2 = 1 r 1


1/2 = 0 r 1





Read the remainders from bottom up: 11010

No comments:

Post a Comment