Hi, beaker. I just wanted to add some more clarification for the bitwise AND operator. Firstly, we've gotta know how to write a target number in binary code. We'll use the numbers 20 and 25 as examples, and it's a good idea to number each of our steps as we go along.
So, how
do we write 20 in binary code, anyhow? We divide the number in question by 2, and see whether our remainder is 0 or 1. Then we take the number we got in the previous step and divide THAT by 2, again checking whether our remainder is 0 or 1. This process continues until we can't divide by 2 anymore. Now, it's time to put my explanation into action - here goes.
How to write 20 in binary code.....
Step 1: We divide 20 by 2 to get a result of 10 and a remainder of 0.
Step 2: Now we divide our previous result, 10, by 2 to obtain 5 and a remainder of 0.
Step 3: 5 gets divided by 2; result is 2 and remainder is 1.
Step 4: 2 divided by 2 is 1; remainder 0.
Step 5: 1 cannot be divided by 2; this is the end of the line. Our result is 0 and our remainder is 1.
Now that we've obtained our remainders for each step, we have to reverse our sequence to get the binary representation of 20.
Step 5 = 1
Step 4 = 0
Step 3 = 1
Step 2 = 0
Step 1 = 0
So, the binary representation of 20 is
1 0 1 0 0.
Now, I'll demonstrate how to convert a binary string into a decimal number. To do that, we need to count how many digits that string has. In the example above, we've got 5 digits. The leftmost digit is always 2 raised to however many digits are in the string minus 1. That is, the leftmost digit is 1 x (2^4). On the other hand, the rightmost digit is always either 0 or 1 times 2 raised to the 0. That said, I'll now explain how in Beelzebub's basement this string gives a result of 20.
1 x (2^4) + 0 x (2^3) + 1 x (2^2) + 0 x (2^1) + 0 x (2^0) which becomes 16 + 0 + 4 + 0 + 0 =
20.
Bada bing, bada boom! We've written 20 in binary code and converted binary code to decimal numbers!
Now it's time to write 25 in binary code.
Step 1: Result 12; remainder 1.
Step 2: Result 6; remainder 0.
Step 3: Result 3; remainder 0.
Step 4: Result 1; remainder 1.
Step 5: 1 divided by 2; no can do! Result 0; remainder 1.
Reversing our sequence, we get:
Step 5: 1
Step 4: 1
Step 3: 0
Step 2: 0
Step 1: 1
25 in binary code is therefore
1 1 0 0 1.
Time to plug and chug to check our math.....
1 x (2^4) + 1 x (2^3) + 0 x (2^2) + 0 x (2^1) + 1 x (2^0) which becomes 16 + 8 + 0 + 0 + 1 =
25.
Now here's where the bitwise AND part comes into play. Let's examine our binary representations of 20 and 25 again.....
20: 1 0 1 0 0
25: 1 1 0 0 1
The bitwise AND operator looks at the binary strings being combined to see whether they've got 0's or 1's at a given position. The basic gist of bitwise AND is that.....
1 and 1 make 1
0 and 1 make 0
0 and 0 make 0 for a given position in a binary string.Proceeding from left to right, we get:
1 0 0 0 0 which becomes.....
1 x (2^4) + 0 x (2^3) + 0 x (2^2) + 0 x (2^1) + 0 x (2^0) or 16 + 0 + 0 + 0 + 0 =
16.
As we can see, bitwise AND works in wacky ways! If we combine 20 and 25 in the decimal system, we of course get 45. But if we combine the binary representations of 20 and 25 in bitwise AND, we end up with a string that equals 16!
Anyhow, I'm sorry if this post was too long-winded and I hope this helps anybody who might be having trouble with the bitwise AND operator.