< Day Day Up > |

## Binary Numbers and Logical OperatorsFor all of my griping in this chapter, the bitwise operators are actually very cool. The bitwise operators let you manipulate the individual bits that make up an integer value. You do so using the operators If you haven't used these operators before, it might sound ridiculous to think of "2 OR 6 = 2" as being a meaningful expression. But it is. For that expression to make sense, we need to examine the bits of a number, which means you need to be able to visualize a base 10 number as a base 2 number. Base 10 numbers are what we use every day. It includes the numbers that we can make using the digits 0-9. Base 2 numbers use only the digits 0 and 1 for representing values. The bitwise operators take a number in base 10 and look at it as its base 2 counterpart and do something with the bit value at the specified position. Still sounds opaque. Let's circle around again. How do you represent a base 10 number as a number in base 2? You only have a 0 and a 1 available to you. What you do is generate a number line that starts with 1 and goes out to the left. The numbers represented on the number line are the powers of 2. Here is such a number line that goes out only a few powers of 2: 64 32 16 8 4 2 1 We get this number line because 2 For example, 3 is a base 10 number that isn't on the number line. We can represent 3 by using only a 2 and a 1, and none of the other numbers on the number line. We can get 22 by using the 16 and the 4 and the 2, and none of the other numbers. If we want to represent a base 10 number in binary, we take the numbers we need and leave the ones we don't. To take a number, we put a 1 under it; we put a 0 under numbers that we don't need to use. So let's do that now. Let's represent 3
If we take the 0s and 1s above as our number, we get 11. 3 Let's do 22 now.
So we represent 22 base 10 in base 2 as 10110. The 1 means that bit is on (or true) and 0 means that bit is off (false). Now we can use this as the basis to perform our logical operations. Table 7-3 shows the outcome of boolean expressions.
In that table, if we substitute 1 for every "true" and 0 for every "false," perhaps you can start to see how we could say something like 3 AND 22. Let's take our base 2 representation of 3 and our base 2 representation of 22 and line them up on top of each other, like this:
Now we just need to draw a vertical line between each bit that makes the 3 and each bit that makes the 22, performing the logical operator AND on each one. Here we go:
So now we've got a 0 (not used) in every place but the 2. The 2 bit has a 1 (true). So, the answer to the expression Let's try another quick one, because it's so fun. What's 13 ^ 6 (13 eXclusiveOR 6)?
We can do this quickly now. Remember that exclusive or means that each operand must be a different value.
The answer is 1 + 2 + 0 + 8 = 11. Here is some Java code that prints out these different values just to prove it. ## BooleanOps.javapublic class BooleanOps { public static void main(String[] args) { System.out.println(3 & 22); //2 System.out.println(13 ^ 6); //11 System.out.println(0 & (3-3)); //0 System.out.println(45 | 16); //61 System.out.println((2 & 5) | ((7^3) & 5 )); //4 } } Now that we can see behind the base 10 and into the bits, we can use the bit shift operators to manipulate the individual bits of a number. |

< Day Day Up > |