Static Zero

Writing What We Want

Static Zero header image 2

All About Binary

April 24th, 2008 · No Comments · Programming, Random

This post contains information on how to convert decimal numbers to binary, 1’s complement, 2’s complement, and binary coded decimal. I’ve tried breaking it up to keep it somewhat organized, so hopefully you can follow.

The Basics - Converting Decimal to Binary
Well, I’ll do what everyone else does and define it.
A piece from dictionary.com:

2. Mathematics.
a. of or pertaining to a system of numerical notation to the base 2, in which each place of a number, expressed as 0 or 1, corresponds to a power of 2.

All that means is that rather than a number in the format we know, called base 10, this number is created using 1’s and 0’s. In decimal, each of our place values is a power of 10. For example, take the decimal number 49.
49 = 4*10^1 + 9*10^0
So, rather than use base 10, lets use base 2.
49 = 1*2^5 + 1*2^4 + 0*2^3 + 0*2^1 + 1*2^0

Now, that seems a bit complicated and might seem like a lot of work. Well if we think about it differently, it will seem easier. First, get comfortable with powers of 2. 1, 2, 4, 8, 16, 32, 64, 128, etc. Once you have those down, you can easily convert a decimal number by subtracting the given 2^power from the decimal number. Take a look at this example:
Write out powers of 2 in reverse order:

bit #   7       6       5       4       3       2       1       0
        2^7     2^6     2^5     2^4     2^3     2^2     2^1     2^0
        128     64      32      16      8       4       2       1

Then, if the number you are trying to convert is greater than the highest number, put a 1 in the slot and subtract it from your number, so with the number 81 it would look like this:
is 81 > 128? No, so 0.
is 81 > 64? Yes, so 1, then subtract 81-64 = 17.

bit #   7       6       5       4       3       2       1       0
        2^7     2^6     2^5     2^4     2^3     2^2     2^1     2^0
        128     64      32      16      8       4       2       1
        0       1

Continue putting 1’s and subtracting whenever your number is higher than the current place value you’re looking at. If it is not, then put a zero. Continue this until your number equals 0.
81-64 = 17
17-16 = 1
1-1 = 0

bit #   7       6       5       4       3       2       1       0
        2^7     2^6     2^5     2^4     2^3     2^2     2^1     2^0
        128     64      32      16      8       4       2       1
        1       1       0       1       0       0       0       1

Converting a Binary Number to 1’s Complement
After reading above, hopefully you can convert any decimal number into binary, right? Wrong. Unfortunately there is no way to convert negative numbers using the process that I showed you above. But here is an idea, what if we split binary numbers in half, and use the highest bit as a “sign bit.” This would mean that -5 could be 10000101, and 5 could be 00000101. That would work, but you would always have to have that extra bit, and if you are doing binary math (I wont go into that here) then you may run into problems. So, the geniuses of math and computer science decided that rather than just use the first bit, they would flip every bit of a positive number to make it negative. This is called 1’s complement binary. Let’s look at an example, the number 8 (to -8 in 1’s complement binary):
Convert 8 to binary to get 01000. Flip all the bits to get 1’s complement (-8) 10111.
That’s all 1’s complement is. Flipping the bits. Nice and simple.
Note - You still need one extra bit out front as the “sign bit” or you could run into problems. For example, what if the number above was 16 and you tried showing that in 5 bits. 16 = 10000(binary), but in 1’s complement 10000 is actually “negative 0,” which makes no sense. Therefore make sure you have an extra bit, so 16 = 010000 (which, in 1’s complement, is in fact 16).

Converting a Binary Number to 2’s Complement
Have you ever had a number and had to add 1 to it? Well guess what, that is all that 2’s complement is. See, what happened is those geniuses in math and computer science realized that if you just flip the bits, you have 2 0’s, one “negative” and one “positive”. Well that doesn’t make any sense at all, does it? So they decided the best way to avoid this was to add 1 to any negative 1’s comp binary number. (This means that the “negative 0″ would no longer exist.) It sounds simple, and it is simple (if you’ve read the above sections). Straight to an example:
Convert the number 38 to 2’s complement (-38):
First, convert 38 to binary.
38-64 = — (0)
38-32 = 6 (1)
6-16 = — (0)
6-8 = — (0)
6-4 = 2 (1)
2-2 = 0 (1)
38 = 010011
Now convert that to 1’s complement (flip the bits)
010011 => 101100
Now, add 1. Note - if you do not know how to do binary math, I won’t be explaining it here. I will say that its basically the same as decimal math (if you’re missing 8 fingers).
101100+1 = 101101(2s comp)

Converting a Decimal Number to Binary Coded Decimal
Well, sometimes you don’t want the number in any of the above binary formats. This is rare, but it happens. So what is an alternative binary format you can put it in? Binary Coded Decimal (BCD). This follows the exact process as in the first section above, except it does it for each digit of the decimal number. Let’s jump straight into it.
Convert the number 1934 to BCD:
First, split the digits

1               9               3               4

Then convert each digit to binary (use 4 bits)
Note - this is “normal” binary, not 1’s or 2’s complement!

1       9       3       4
0001    1001    0011    0100

And that’s it. 1934 = 0001100100110100(BCD)

Well, I hope this was helpful. Leave any comments below.

Tags: ·····

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment