Answer» What's the easiest formula to use to convert base 10 to base 2? Comparison with descending powers of two and subtraction
Let's convert the decimal number 156 to binary. What is the greatest power of two that will fit into 156? Since 128 fits, write a 1 for the leftmost binary digit, and subtract 128 from your decimal number, 156. You now have 28.
Move to the next lower power of two. Can 64 fit into 28? No, so write a 0 for the next binary digit to the right.
Can 32 fit into 28? No, so write a 0.
Can 16 fit into 28? Yes, so write a 1, and subtract 16 from 28. You now have 12.
Can 8 fit into 12? Yes, so write a 1, and subtract 8 from 12. You now have 4.
Can 4 (power of two) fit into 4 (working decimal)? Yes, so write a 1, and subtract 4 from 4. You have 0.
Can 2 fit into 0? No, so write a 0.
Can 1 fit into 0? No, so write a 0.
Since there are no more powers of two left, you are done. You should have 10011100. This is the binary equivalent of the decimal number 156.
The answer is the remainders read from the bottom up. Hi,
You could try this example , that algorithm is the best in my opinion:
http://mathbits.com/mathbits/compsci/Introduction/frombase10.htm
Something like : 31 is ?
31 : 2 = 15 r 1 15 : 2 = 7 r 1 7 : 2 = 3 r 1 3 : 2 = 1 r 1 1 : 2 = 0 r 1
So 31 = 11111 ( hmmm one more example , a better example could be... shown)
Let's say.... 20
20 : 2= 10 r 0 (20 = 2 * 10 + 0 ) 10 : 2= 5 r 0 ( 10 = 2*5 + 0 ) 5 : 2 = 2 r 1 ( 5 = 2*2 + 1 ) 2 : 2 = 1 r 0 ( 2 = 2 * 1 + 0) 1 : 2 = 0 r 1 ( 1 = 2*0 + 1 )
So 20 = 10100
Execute those divisions until 0 (as floor) appears in front of r , and your number is constructed from "r" part ( remainder) which should be read from bottom to top.
In those link ALSO this algorithm is explained , may be better how i do it. (any way this algorithm could be used for any number base conversion )
Regards,
Easy? Use a calculator. http://www.tucows.com/preview/414618 Or a table. http://www.ascii.cl/conversion.htm No formula is needed. Binary numbers has the same value as decimal numbers.
OK. Just joking. But is seems rather SILLY to ask on a forum. Can't you do this is your head? 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 and so on. In my opinion you should know how that algorithm works, so that if necessary you could program it, and it will be computer's job to calculate, ", 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 and so on." but you should know how to tell it to do it.
I believe that this question is really a good one.
Then let's clarify the question. An input source has some numbers in ASCII format that represent a number using base ten, sometimes called decimal. We shall change this sequence of symbols into a binary number that represents the value of the visible symbols that most humans understand. (Assembly language programmers are almost human, but that is another topic.)
What have to understand the visible representation of number ant the absolute binary value.
In this example we suppose the the number does not have a decimal point or does not have anything to the right of the point. And the number is less that 32,768 if we want to limit this to a 16 bit operation.
First, we chose a register or variable in memory large enough to hold the binary value. In this case a two byte memory or register. We start with a value of zero. Next we start to the far left of the string of decimal symbols. We subtract 48 to convert the ASCII symbol to a corresponding absolute binary value. This will be done for each digit in the string. but before we do the cent digit, if there is another, we multiply our temp value by ten before we add the value of the current digit.
Using a PSEUDO code, it might look link this. Code: [Select]set 16bit location to zero. call is result set a Bolean to false and call it done. while NOT done get current digit, subtract 48, store in a temporary place. call it temp multiply result by ten add temp to result incitement digit pointer if pointer beyond last digit then let done be true loop use result as binary value. Now then, do you want to represent the binary value as a string of 1s and 0s ? That's another exercise.
But did you want to BCD? Packed or unpacked?
Do you need to understand this? I don't know. You can go through life and be happy without needing to understand the reson roe using numbers systems other that base ten.
We must understand, however you represent a number, it is a number and a number is number. for example the value fifteen and be: 15 base 10 0F base 16 0000 1111 base two or even like this:
If you are using any fomr/mutation of BASIC you can use BIN$(x).
So, to make a QUICKBASIC base10 --> base2 program, use something like this:
CLS INPUT "Enter base 10 number:", x PRINT "Binary form: " + BIN$(x) DO WHILE INKEY$ = "" LOOP ENDQuote from: Fleexy on March 19, 2010, 05:05:19 PM If you are using any fomr/mutation of BASIC you can use BIN$(x).
So, to make a QuickBASIC base10 --> base2 program, use something like this:
CLS INPUT "Enter base 10 number:", x PRINT "Binary form: " + BIN$(x) DO WHILE INKEY$ = "" LOOP END
"BIN$" or "BIN" functions do not exist in QBASIC, QuickBASIC, or any dialect of Visual Basic 1 through 6.
It's probably exclusive to a single dialect, such as RealBasic.
here are two functions that I never use that Ifound in one of my modules (VB6):
Code: [Select]Public Function BinaryToDecimal(ByVal BinValue As String) As Long Dim lngValue As Long Dim X As Long Dim K As Long K = Len(BinValue) ' will only work with 32 or fewer "bits" For X = K To 1 Step -1 ' work backwards down string If Mid$(BinValue, X, 1) = "1" Then If K - X > 30 Then ' bit 31 is the sign bit lngValue = lngValue Or -2147483648# _ ' avoid overflow error Else lngValue = lngValue + 2 ^ (K - X) End If End If Next X BinaryToDecimal = lngValue End Function
and the reverse:
Code: [Select]Public Function dec2bin(mynum As Variant) As String Dim loopcounter As Integer If mynum >= 2 ^ 31 Then dec2bin = "Number too big" Exit Function End If Do If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then dec2bin = "1" & dec2bin Else dec2bin = "0" & dec2bin End If loopcounter = loopcounter + 1 Loop Until 2 ^ loopcounter > mynum End Function
Oops, sorry, got it mixed up with HEX$ and OCT$. Disregard my post please.
...probably because I made my own sub BIN$ (can't find it now, must be on my other computer)
|