It's always fun to learn some additional tricks in math and computer science and I recently started reading about digital representation of numbers and Swarthmore has some truly great material on this.
I was searching around the internet on how to programatically convert decimal numbers to binary and I found there wasn't that great of a resource and stackoverflow had mixed stuff so I thought I'd put it something together quickly.
I'll probably add to this tutorial later but I think the code is the most important part.
In short, we want to represent a decimal number (i.e., a number represented in base 10 via the 10 digits we use [0-9]), e.g., 86, as a binary number (i.e., a number represented in base 2 via the 2 digits we are then limited to [0 and 1]).
As outlined in the tutorial above, this results in the two representations:
Which is equivalent to saying
The subscript denotes a binary number. Each digit in a binary number is called a bit. The number 1010110 is represented by 7 bits. Any number can be broken down this way, by finding all of the powers of 2 that add up to the number in question (in this case 26, 24, 22 and 21).
So how do we do this with a computer?
Binary Representation in Python
As we saw before, representing 86 in binary format requires us to deconstruct it from , so with computers we can represent 86 by either recursively dividing by 2 (with some additional adjustment) or iteratively (via a while loop) doing pretty much the same thing. Here's the recursive version:
1def decimalToBinary(n: int) -> str: 2 if n == 0: 3 return "" 4 else: 5 return decimalToBinary(n // 2) + str(n % 2)
This is pretty simple and could be trivally mapped to a one-liner but for readability I'll say this is sufficient. Alternatively, we could solve this with a while loop using the following:
1def decimalToBinary2(n: int) -> str: 2 bs = '' 3 while n > 0: 4 r = n / 2 5 n = n // 2 6 bs = str('1' if r % 1 > 0 else '0') + bs 7 8 return bs
And this is also pretty simple. It's worth noting that the str() function call is required before concatenating the binary string variable (i.e., bs) because without it python will actually fail to execute the if-else logic and you will not get the behavior you are looking for.
Decimal Representation from Binary in Python
Okay, so how do we confirm this is behaving correctly? Obviously we can just recover it.
1def binaryToDecimal(bs: str) -> int: 2 n, r = len(bs), 0 3 for i in range(n): 4 r+= int(bs[i]) * 2**(n - i - 1) 5 return r
And that's it! If you stare at this formula for a second you'll see it's just taking each bit in the string and multiplying the binary value by .
We can verify that all of this works with some simple checks and comparisons:
1decimalToBinary(86) == decimalToBinary2(86) # 1010110 == 1010110 --> true 2binaryToDecimal(decimalToBinary2(86)) == 86 # true, converted 1010110 -> 86
How cool is that? I'll note that this is only accurate for positive integers but for the general case you can use the first digit to represent the sign of the numbers (with some handling for the 0 edge case).
Hope you found this interesting. As I said I'll elaborate more on this post eventually but I thought I mostly wanted to share the code as I didn't really see good examples providing this back and forth and it was something that helped me understand things more concretely.