Wikipedia:Reference desk/Archives/Mathematics/2015 May 21

From Wikipedia, the free encyclopedia
Mathematics desk
< May 20 << Apr | May | Jun >> May 22 >
Welcome to the Wikipedia Mathematics Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


May 21[edit]

How To Convert Decimal To Binary?[edit]

Hello. I Am trying ton convert a Decimal Number (say, 16) to Binary. The article on binary numbers says, "To convert from a base-10 integer numeral to its base-2 (binary) equivalent, the number is divided by two, and the remainder is the least-significant bit. The (integer) result is again divided by two, its remainder is the next least significant bit. This process repeats until the quotient becomes zero." I don't understand how to do this. Could some one explain step by step how to convert the number 16 to binary? The correct binary representation of 16 is 10000. Thanks in advance —SGA314 (talk) 15:10, 21 May 2015 (UTC)[reply]

16 divided by 2 is 8 with remainder 0.
8 divided by 2 is 4 with remainder 0.
4 divided by 2 is 2 with remainder 0.
2 divided by 2 is 1 with remainder 0.
1 divided by 2 is 0 with remainder 1.
So the digits of the binary representation of 16 *from right (least significant) to left* are 0,0,0,0 and 1. Reversing this to write the binary representation from left to right we get 10000. Gandalf61 (talk) 15:21, 21 May 2015 (UTC)[reply]
The method I use is to find the largest power of 2 less than or equal to the number, in this case 16, then subtract that:
16 - 1 sixteen = 0
I then continue with each smaller power of 2:
0 - 0 eights = 0
0 - 0 fours  = 0
0 - 0 twos   = 0
0 - 0 ones   = 0
That gives me 10000. I find it easy to use my fingers to record the binary digits, allowing for binary numbers up to 1111111111, or 1023 in decimal. StuRat (talk) 15:32, 21 May 2015 (UTC)[reply]
  • Stu's way may show more clearly what the representation means, but Gandalf's is quicker: no need to figure out first what the biggest power is, nor to keep track of what to subtract this time. —Tamfang (talk) 05:31, 24 May 2015 (UTC)[reply]
I tend to show people that I don't like the fact that I can count to 132. :)Naraht (talk) 16:17, 21 May 2015 (UTC)[reply]
Ok Gandalf61, I understand. But how would I do this using a calculator, considering the fact that a calculator will divide into fractions and have no remainder(as in the case of dividing 1 by 2 which yields 0.5). My reason for asking this is because I am writing a program that can convert a decimal number to a Binary number and the reverse. Oh and i am using Visual Basic For Applications as my programming language. —SGA314 (talk) 18:24, 21 May 2015 (UTC)[reply]
Programming languages normally provide a modulo operator that gives you the remainder that division would produce. According to the article I linked, in Visual Basic that operator is named Mod, and this page from Microsoft tells you how to use it. --174.88.135.200 (talk) 19:11, 21 May 2015 (UTC)[reply]
Cool Thanks! problem solved. Now I can convert decimal numbers to and fro binary! Thanks for your help everyone. —SGA314 (talk) 19:15, 21 May 2015 (UTC)[reply]
Here is the final VB script i came up with. if you have any suggestions to improve/simplify it let me know. Output from this script is 110001.
Sub ConvertDecToBin()
    'Decimal To Binary Conversion Script.
    'Written By SGA314
    Dim Num, Remain, Times, i, Result As Double
    Dim BinNum As String
    Num = 49 'Insert number to convert to binary here.
    Times = Num / 2
    Result = Num
    While Not Result = 0
        Remain = Result Mod 2
        BinNum = BinNum + CStr(Remain)
        Result = Int(Result / 2)
    Wend
    Debug.Print StrReverse(BinNum)
End Sub

SGA314 (talk) 20:15, 21 May 2015 (UTC)[reply]

I don't have time to check your code, but you may be interested in seeing how this example works [1]. SemanticMantis (talk) 20:24, 21 May 2015 (UTC)[reply]
It's been a while since I've written in VB, and it was mostly in normal VB rather than VBA, but:
  1. Not sure how you intend to use this, but hardcoding the value to convert makes little sense. It would be more meaningful to have the value given as a parameter. It might also make more sense to have it a Function instead of a Sub, and print the result in the calling sub.
  2. You have values you're defining but never using.
  3. The code isn't equipped to handle fractions, so unless you want to modify it to do that, it would make more sense to work with ints rather than doubles.
  4. The \ operator represents integer division in VB, so you can replace Int(Result/2) by Result\2.
So you can use the following code instead:
Function ConvertDecToBin(ByVal Num As Int) As String
    'Decimal To Binary Conversion Script.
    'Written By SGA314
    Dim Remain As Int
    Dim BinNum As String
    While Not Num = 0
        Remain = Num Mod 2
        BinNum = BinNum + CStr(Remain)
        Num = Num \ 2
    Wend
    Return BinNum
End Function
-- Meni Rosenfeld (talk) 21:00, 21 May 2015 (UTC)[reply]
Many languages will also contain a built-in method of converting, sometimes as simple as assigning a decimal value to a binary variable. Of course, coding it yourself is also useful for educational purposes. StuRat (talk) 03:40, 22 May 2015 (UTC)[reply]

In J (programming language) you write

   #:16
1 0 0 0 0

Bo Jacoby (talk) 06:12, 22 May 2015 (UTC).[reply]

I think I can truly say I have never used a variable of decimal type. —Tamfang (talk) 05:31, 24 May 2015 (UTC)[reply]
Oops. Your right Meni Rosenfeld, I forgot to remove the Times and i Variables. These were variables that I used for a different way of converting (I was using a For Loop). And as for using Doubles instead of Integers, I am using doubles because they can hold values much higher than 32,768(I can store numbers that go into the Billions range with Doubles) and if I try to store any value higher than 32,768 in an Integer value, I will get an Overflow error. Good idea by the way on making it a Function that returns a String. I coded this as a macro in Microsoft Word, so I was going replace the hardcoded number with Selection (Selection is how you get the currently selected text in Word) instead. As for the Int(Result / 2) line, I don't know whats up with this. I don't know if it isn't necessary in VB, but in VBA if I don't include this the program will spit out
000000000000000000000000000000000000000000000000000...(a huge amount of zeros that take up way to much space on the page)1010001, instead of 110001. But if i use your version of the script(with a few mods to make it compatible with VBA) it will work just fine. I have to say, I like how you simplified the code and used 1 less variable than I used. I guess VB is a lot different from VBA, because in VBA for Word, I cant put a variable after the Return statement (I utterly disgust this). Instead I have to do this
Function A() As String
and in order to return something I have to set a variable that has the same name as the function (no I don't have to define it) like so
A = "Whatever"
this will return the String "Whatever."
That would be nice StuRat. But I don't think VBA has a function like that but that sure would be cool if it did. I thank you very much for your feed back! Here is my simplified version of the script(special thanks to Meni Rosenfeld for this)
Function ConvertDecToBin(ByVal Num As Double) As String
    'Decimal To Binary Conversion Script.
    'Written By SGA314
    'Improvments By Meni Rosenfeld
    Dim Remain As Double
    Dim BinNum As String
    While Not Num = 0
        Remain = Num Mod 2
        BinNum = BinNum + CStr(Remain)
        Num = Num \ 2
    Wend
    ConvertDecToBin = StrReverse(BinNum)
End Function

Thanks for your help everyone! I now understand how to convert a decimal number to a binary number and do the reverse. —SGA314 (talk) 13:30, 22 May 2015 (UTC)[reply]

I think the differences are less between VB and VBA, and more between modern vb.net (on which one can easily find documentation), and classic VB <= 6 (on which I think VBA is based). This goes for how to return values from a function, and also for integer data types. In classic VB, Integer refers to a 16-bit signed int, which can go up to 32767; But you also have Long which is 32-bit and goes up to ~4 billion (which might be good for you instead of Double). In the modern version, Integer is 32-bit, and Long is 64-bit and gives you much higher precision than Double. -- Meni Rosenfeld (talk) 14:53, 22 May 2015 (UTC)[reply]
Huh Good to know. Thanks. —SGA314 (talk) 15:56, 22 May 2015 (UTC)[reply]
Note, this is also something that you may consider using a recursive program on. Something like (Visual basic was a while ago)
Function RecursiveConvertDecToBin(ByVal Num As Double) As String
    If Num > 0 Then
        RecursiveConvertDectoBin = RecursiveConvertDectoBin(Num\2)&CStr(Num Mod 2)
    Else
        RecursiveConvertDectoBin = ""
    End If
End Function
Naraht (talk) 22:35, 22 May 2015 (UTC)[reply]
I dunno nothin' about VB, but I'd expect recursion (in a sane language) to slow things down considerably. —Tamfang (talk) 05:31, 24 May 2015 (UTC)[reply]
Not necessarily, fewer variables, more copies. Worth checking at least. :)Naraht (talk) 19:02, 26 May 2015 (UTC)[reply]
Can I suggest an even simpler method? Simply type for instance "123 in binary" into Google and it'll tell you. ;-) Dmcq (talk) 09:15, 24 May 2015 (UTC)[reply]
The problem with that is that i can't use Google. —SGA314 (talk) 14:19, 25 May 2015 (UTC)[reply]