Step 2: Base Basics (Con't): Binary

Binary (base-2)

As the name implies, a binary numbering system consists of two numbers: 1 and 0. Accordingly, this is referred to as base-2 and numbers look like 100100102. In such a system, the representation of values are accomplished through a "power of two" system. Each place in a binary number, often referred to as a bit, has a particular value that can easily be determined. Binary numbers are usually written as a multiple of eight, or a byte. Sometimes, only four bits of the byte, either the upper or lower half are used and these four bits are collectively called a nibble. Nibbles become important when converting between bases as we will see later.

Without digressing, I will be using big endian notation where integers are stored in memory starting with the most significant bit (MSB) and ending with the least significant bit (LSB). The value of any given bit location is determined by taking two (the number of digits in our binary system) to the power of the bit's place using a zero-based index: 2bit. For instance, the LSB of the nibble is 20 or 1. The fourth bit, that is, the third place (remember we start at zero) value is 23 or 8. Let's just look at some examples to make it clearer. Here's how a nibble is represented in binary.

Zero-based index    3   2   1   0
Bit place          4th 3rd 2nd 1st
Bit value           8   4   2   1
Example values:

Decimal 12          1   1   0   0
Decimal 5           0   1   0   1

If the bit place's value is to be added (or counted) that bit will be a 1, else it will be a 0. Knowing this, the decimal value "1" is written "0001" because the 1st bit is set and that value is one. The decimal value "10" is written in binary as "1010" and if you add the bit place's value where there is a 1 then you will see it adds to 10. In this way, a nibble can represent the numbers 0 through 15. Larger numbers require the use of the high-order nibble.

Bit place    8th 7th 6th 5th 4th 3rd 2nd 1st
Bit value    128  64  32  16  8   4   2   1
Example value:
Decimal 88    0    1   0   1  1   0   0   0
Decimal 253   1    1   1   1  1   1   0   1

Add the bit values for each bit's place that has a 1 to satisfy yourself that the decimal values are accurately represented by the binary value to its right.

I will cover binary addition, subtraction, multiplication, division, floating points, and negative numbers via 2's complement and 1's complement in a later instructable I've already started working on called How to Calculate like a Microcontroller, but here's a preview as an enticement (yeah, like binary math can be an enticement, right?) to view and rate my other instructables. :)

Preview of How to Calculate Like a Microcontroller.

Binary Arithmetic: Adding and Subtracting
Binary arithmetic is very simple and straight-forward. You should familiarize yourself with the binary representation of numbers zero through three, if you don't already have these committed to heart. They are 00, 01, 10, and 11. The primary thing to remember is to carry over ones and that's the hardest thing you'll have to do, so breathe easy.

I'm going to add 15810 and 5110 in binary, which if you have already added them should yield 20910. Follow along and double-check my calculations.

     1001 1110
   + 0011 0011
Now, add down from the LSB (far right) moving to the left. The first is 0 + 1 which is 1. The next is 1 + 1 which is 2, but in binary is 10. This is two digits so we have to carry that one over to the next column.
     1001 1110
   + 0011 0011

In that column, it's 1 + 0 + 1 which is again two, or 10. Carry that one. Continue.
     1001 1110
   + 0011 0011
Now we have 1 + 1 + 1 which is three, or, yes, you got it: 11. So instead of putting a zero and carrying a 1, put a 1 and carry a one. Continue to the end.
     1001 1110
   + 0011 0011
     1101 0001
The 110100012 is 20910, so we did our calculations correctly.

Binary Arithmetic: Multiplication and Division
In the base-2 system, division and multiplication by a power of two is also easily accomplished by right and left shifting, respectively. Shifting is exactly as it sounds: when you left shift by n, you take all the 1's and 0's and move them n places to the left filling in the shifted places with a 0. When you logically right shift by n places, you move all the binary values n places to the right, filling the moved slots with 0's (there is also an arithmetic right shift, but I don't cover it here). In the example below, I shift the number 4 to the left by one bit. The bit #1 is the 2nd from the right in our zero-based index system, and we can also write it 21 or 2. This should cause a multiplication of 4 * 21 or 8.
Left shift:   4 << 1
Bit value:    8 4 2 1
Binary: 4 =   0 1 0 0
Shifted to the left one bit:
              1 0 0 0  = 8 denary

You can see how shifting to the left changes the number from four to eight, effectively multiplying the value by two. We could also shift left by two bits or any number of bits from 1 to n - 1, where n is the number of bits in our binary number. Accordingly, shifting a binary number to the right effectively divides the number by a power of two. Think about that if you're not clear then take a look below. Here we shift 32 two bits to the right, effectively dividing 32 by 22 , yielding 8.

Right shift: 32 >> 2
Bit value:  128 64 32 16 8 4 2 1
Binary 32 =  0   0  1  0 0 0 0 0
Shifted to the right two bits:
             0   0  0  0 1 0 0 0

That's far from all of the foundational binary operations, but it's an adequate coverage for our lil' 'ol instructable here.
<p>And here you can find the formal math to convert number in any base to any other base: http://www.codinghelmet.com/?path=exercises/converting-number-bases</p>
<p>Hiyas,</p><p>Thanks for that link. Webpages that make use of latex math please me. :)</p>
<p>Alright, my question is about decimal to hexadecimal. When you have the number (9 in your example) and divide it by 16 and it equals 0, it works fine. But when you have a number like 429dec, which provides 26, which divides by 16 to 1, what do you do. I tried multiplying the equation by 1 and moving it over, and the converter showed a different result.</p>
Hi Zack, Ok, if I understand what you're unsure about, let's take the number you gave and work it out using my method.<br> <br> 429<sub>10</sub> = ???<sub>16</sub>&nbsp;<br> <br> 1. 429 / 16 = 26.8125; .8125*16 = 13; 13 in hex is D. So 0x??D<br> 2. 26 / 16 = 1.625; 0.625 * 16 = 10; 10 in hex is A. So 0x?AD<br> 3. 1 / 16 = 0.0625; 0.0625 * 16 = 1; 1 is 1 in hex, so 0x1AD.<br> 429(dec) = 0x1AD(hex)<br> <br> Hope that helped to clear it up. If you still have problems feel free to ask again.<br> Cheers and thanks for the comments!
<p>Hey guys,</p><p>Thanks for the comments. I checked and it looks like a typo as I have it correctly typed in the preceding line. The typo below is now corrected.</p><p>Thanks for finding that!</p>
<p><strong></strong></p><p>158 base 10 should be equal to 314 in base 7.</p>
<p>I second/confirm/approve of or whatever this statement.</p>
<p>Good Job. Please confirm the conversion of <strong>158 (Base 10) into base7 the result it should give is 314 (Base 7), not 214 (Base 7). </strong></p><p><strong>Just cross-check if there were any errors during calculations.</strong></p><p><strong>Weldone !!!</strong></p>
<br> You left out octal (base 8), which is just like base 10. If you're missing two fingers.<br> <div class="media_embed"> <a href="http://video.google.com/videoplay?docid=-7841878207694220233#">http://video.google.com/videoplay?docid=-7841878207694220233#</a></div> <br>
Oops, where did I leave out octal? <br><br><br>Ah, Tom Lehrer....
oops. I guess not. It was just missing from titles...<br>
where did you hear that joke from?
I think I saw it first on a thinkgeek t-shirt.

About This Instructable




Bio: Gian is a computational biologist and is the Managing Director at Open Design Strategies, LLC. He holds a BA in Molecular/Cellular Biology and an ... More »
More by nevdull:Create A Custom Medieval-/Fantasy-Style Calligraphy Quill Practical DACs Using Enumerated Types as Bitflags 
Add instructable to: