AS: Binary and Bitwise operators 2006-01-01 18:48:15
So, I didn't find any tutorial on binary numbers in AS: Main, so I thought I'd have a go. It's not really AS related, but heck, it's worth a shot =P. I'm using Flash MX 2004 for this, but it really shouldn't be a problem, as all the operators works on Flash 5 and above.
What is binary?
Binary is the way that computers sees numbers, in simply one's and zero's, giving it the advantage of being able to be represented by a number of booleans (or switches, in computers). An example of a binary number would be 100011, which represents 35.
The way we're used to see numbers is based by ten's. So the number 32604 consists of, from left to right, 3 x 10000, 2 x 1000, 6 x 100, 0 x 10, and 4 x 1. In other words 30000 + 2000 + 600 + 4 = 32604. But instead of saying 3 x 10000 and so on, it could be said as 3 x 10^4, and then 2 x 10^3 and so on, as it's ten based. Converting the number 32604 would look like this: 111111101011100. Representing, from left to right, 1 x 2^14, 1 x 2^13 and so on, as it's two based. So, taking on a number a bit smaller, like 9 in decimal (ten based), would be 1001 in binary (two based), consisting of 1 x 2^3 (8) and 1 x 2^0 (1), as in 8 + 1, that equals 9. If you like to have it visualised, .here's a picture to make it easier to understand.
You could think of it as a number of bowls, in which you either put one stone (1), or no stone(0). Each bowl would represent two times the previous bowl, so staying at the number 9, 1001 would be four bowls, the one most to the right would represent 1, the next 2 (1x2), the next 4 (2x2) and the last 8 (4x2).
Here's a converter, in which you can simply try a number of numbers, and see if it was the way you expected, if not, then look closer at the binary number, thinking of it as bowls. Hexadecimal numbers is based on 16 instead, using the numbers 0-9 and A-F, so the first bowl would be how many 1's (16^0), the next would be how many 16's (16^1), the next how many 256's (16^2), and so on.
So now, when you (hopefully) understand binary, on to the bitwise operators.
The two main bitwise operators are << (bitwise left shift) and >> (bitwise right shift). First, I'll explain the bitwise left shift operator, as it, in my opinion, is simpler to understand.
Bitwise left shift
First, an example. 8 << 10 equals 8192. You can try it in flash using
trace(8 << 10);
Now, explaining it. 8 in binary is, as you hopefully could figure out, 1000. So, left shifting it by 10 basicly means adding 10 zero's to the end of the binary number, and converting back to decimal, now having 13 zero's (2^13). Try trace(Math.pow(2, 13)); in flash, and you'll hopefully get 8192.
Taking a simpler example, 8 << 1, is the same as adding 1 zero at the end, so 1000 becomes 10000. And you have hopefully figured out that that will move the "stone", if you think in bowls, one bowl to the left. And as every bowl represents twice the value of the one before, 8 (1000) becomes 16 (10000).
That pretty much covers the left shift, on to the right.
Bitwise right shift
If you understood the left shift, this shouldn't be to hard to grasp. Instead of adding zero's at the end, you remove numbers at the end. So 8>>1, in other words, removing one number at the end of 1000, would be 100 in binary, and 4 in decimal. So far, so good. But if a one gets in the way, then what? Take 9 for example, 1001 in binary. if you do 9>>1, that would remove the first digit, which in this case is a 1 instead of a zero. So that also becomes 100 in binary, and 4 in decimal. Try it yourself, with this code:
trace(8>>1); // Outputs 4
trace(9>>1); // Also outputs 4
So 8>>10, in other words removing 10 digits from 1000, would, not too surprisingly, become 0.
There are two more bitwise operators, bitwise right shift and assignment and bitwise left shift and assignment, that pretty much is a shortcut. They are written like this: >>= and <<=. Looking at the example below should make things clear.
var a:Number = 8;
trace(a); // Outputs 16
var a:Number = 8;
trace(a); // Outputs 4
I'm pretty new to all the bitwise operators, so if I missed anything, or said anything wrong, just point it out. And also, you can ask any questions and I'll try to answer them =D