IAN LANG ELECTRONICS
I must begin this treatise with an apology. Binary is a deathly dull subject. It's completely boring, to the point where one begins to wish one had a handy revolver to hand so that one can shoot oneself through the head. Bitwise is far, far worse. It's worse than pulse width modulation. It's even worse than the theory of relativity. I would go so far as to say that were it not for football, golf and tennis, Bitwise would be the most boring thing in the world. It's worse than being forced to listen to Radio 1 all afternoon after having your earplugs confiscated. Yes it is that bad.
Nevertheless, Bitwise is important if you want to be the master of your Arduino board, and to understand bitwise we need to understand binary fully. Have a good yawn, get it out of your system, go and make a big cup of tea and if you smoke make sure you've got at least twenty to hand because from here on in it's tedium all the way.
Now, we've looked at the binary system of counting before now, but not in great depth. Binary is a way of counting using a unitary value and the powers of 2. Given a finite number of binary digits, we can represent any decimal number up to the maximum power of two plus the unitary value that that finite number of digits allows, using a system of counting in zeroes and ones.
Look, I told you this was going to be boring, didn't I? Let me put the above in plain English and elaborate on the statement so that It doesn't look quite so much like an A level maths teacher nearing retirement.
Let's consider the number 10. When counting normally, we count in decimal. So 10 is in words ten, and is equal to 1+1+1+1+1+1+1+1+1+1 and there's nothing complicated about that, we all learnt that in primary school. Decimal is also known as base ten, because the system is based on the numbers 0 to 9, and if you count from 0 to 9 there's ten discrete digits there. Binary is also known as base two. If you count from 0 to 1 you'll find there's two digits there. Just as in decimal 9 is the highest digit, in binary 1 is the highest digit.
If you find this a difficult concept, consider the workings of the decimal system. First there's zero. Counting up, you get to 9, and that's the highest number of units you can have before you get to tens. 10 follows, and then you keep counting, and get to 19, and that's the highest number in the teens you can have before you get to twenty, and 29 is the highest number in the twenties you can have before you get to thirty. 99 is the highest number you can have before you have to use 100, and then you have to go to 1000, and 9999 is the highest number you can have in the thousands. See what's happenning here? Every time you get a 9 at the end of the number, in the next number up the last digit returns to zero and you put the next digit to the left up by one. If the next digit to the left is greater than nine when you put it up by one, that too returns to zero and the digit to the left of that goes up by 1......... 089 (count up one becomes) 090 and 099 (count up one becomes) 100 199 (count up one becomes) 200. You can go on like this ad infinitum until you reach vast meaningless numbers but your friends will desert you and your family will be looking for a good mental hospital if you do. The way it goes is if you consider the number eight thousand four hundred and fifty six you'd write it out in figures as 8456 (or 8, 456 if you're doing it the old way).
Breaking those columns of digits down you get:
THOUSANDS HUNDREDS TENS UNITS
8 4 5 6
Binary works in a similar way. The highest number we can have in any column in binary is not 9, it is 1, and every time we get a 2, we return that column to zero and look to the next column to increment it.
In decimal we work to powers of ten. Ten to the first power is 10. Ten to the second power is 10 X 10 or 100, ten to the third power is 10 X 10 X 10 or 1000. The easiest way to remember is whatever power it is, stick that many zeroes after the 1. You can also have ten to the power of zero, which is 1. In fact any number whatsoever to the power of zero is 1. 1 is also known as unitary value.
In binary we do not work to powers of ten. We work to powers of two. Two to the power of zero is 1. Two to the power of 1 is 2. Two to the power of 2 is 4, two to the power of three is 8, and to the power of four sixteen and so on. If we have two binary digits we can now express them four ways:
00, 01, 10, 11
Okay, let's look at how that breaks down. Our columns are :
Well, we know 2 to the power of zero is in fact 1, and to the power of one is two. Where there is a 0 in that column it means that that number is not existent in the figure as a total. Yes, I know, don't worry it'll become clear later. In this case, both columns have a zero and so neither two to the first power or the power of zero exist in the total. This means the whole thing in fact equals 0 .
The above example shows that two to the zero exists in the total as a whole. Okay, that's 1 then. Two to the first power does not exist. So all we have is a 1, and that's the total- just 1.
Above we see that two to the zero does not exist in the total as a whole. Fine, let's ignore it completely. Two the first power does exist and that's 2. That's our total, then- just 2.
In the above example two to the zero does exist in the total as a whole. So we have 1. Two to the first power exists too. That's 2. So add them together and we get a total of three.
Spiffing, now we can count up to three. What about four? We put in another column, like so:
2 2 2
1 0 0
And here we see that neither two the power of zero or one exist in the total but two to the power of two (2 X 2) does. And so our result is 4. Similarly five would be 101, six would be 110, and seven would be 111. If want eight we add another column, which is 2 to the third power (2 X 2 X 2 or eight) : 1000 and nine is 1001, ten 1010, eleven 1011, twelve 1100, thirteen 1101, fourteen 1110, fifteen 1111.
The number of digits you have is the maximum number you can count up to and each digit is known as a bit (Binary dig IT).
Here's some code for your Arduino that illustrates the principle. Open your serial monitor and it converts decimal to binary.
int incoming; //set variable
Serial.begin(9600); //open for serial communication
Serial.print(" = ");
In the first yawn above we had four bits and were able to count from any number from 0 to 15. If we had eight bits, we could count from any number between 0 and 255. (2 to the eigth power = 256). 8 bits is called a byte, because historically this was the least number of bits needed to represent an alphabetic character on a computer. Four bits are known as a nibble. Who says that computer spoffs have no sense of humour? You'd expect a kilobyte to be 1000 bits. It isn't though. It's 1024, the tenth power of 2. Don't blame me, I didn't invent it.
Your Arduino board, or more specifically the ATmega that sits on it, uses 2 bytes or sixteen bits if you like, to store a number. That's why a variable of type int can hold a number between -32,768 and 32,767 and why an unsigned int can hold 0 to 65,535 (the latter being 2 to the sixteenth power minus one).
It does this in binary, using sixteen of the columns (or bits) as described above. Here's a sketch to upload to your board. Once uploaded, open the serial monitor and send a character from your keyboard by typing it into the text box at the top of your serial monitor and hitting the enter key. It'll return a message telling you the ASCII code (what your computer recognises that character to be in decimal) and the binary value.
Serial.print(" which in decimal is ");
Check the binary values it gives you, and they should correspond to the decimal values given. Start at the right and side and work your way left. Remember the columns are in powers of two, and go:
128 64 32 16 8 4 2 1
Which is eight bits if you count them.
The reason that digital devices such as your Arduino board work in binary is that it is very conducive to a Boolean argument. A Boolean argument states that a condition is either true or false. A digital device knows only whether a voltage is present at the input or not. If there is that's true, represented by a 1, and if there isn't that's false and represented by a 0.
That's the top and bottom of binary, it's really not that hard once you've got the hang of it. Now on to something that is. Next page for bitwise, which is truly, truly awful.