Previous Section  < Day Day Up >  Next Section

Binary Numbers and Logical Operators

For 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 & (AND), | (OR), and ^ (XOR, the EXCLUSIVE OR). Using these operators with an integer value lets you determine the value (0 or 1) for the bit in the specified position.

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 20 is 1, 21 is 2, 22 is 4, 23 is 8, and so on through 26. Now we can use this number line to take values from. We can represent any base 10 number between 0 and 127 using these numbers. We can do that by adding different numbers together.

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 310 as 32.

64

32

16

8

4

2

1

 

0

0

0

0

0

1

1

(1 + 2 = 3)


If we take the 0s and 1s above as our number, we get 11. 310 = 112. So our answer is 11.

Let's do 22 now.

64

32

16

8

4

2

1

 

0

0

1

0

1

1

0

= 2210 (because 0 + 2 + 4 + 0 + 16 = 22)


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.

7-3. Boolean Operator Expression Results

AND

true AND true = true

true AND false = false

false AND false = false

OR

true OR true = true

true OR false = true

false OR false = false

XOR

true XOR true = false

true XOR false = true

false XOR false = false


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:

64

32

16

8

4

2

1

 

0

0

0

0

0

1

1

= 3 (1 + 2)

      

AND

 

0

0

1

0

1

1

0

= 22 (0 + 2 + 4 + 0 + 16)


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:

1: 1 AND 0 = 0

2: 1 AND 1 = 1

4: 0 AND 1 = 0

Skip 8 because it isn't used.

16: 0 AND 1 = 0

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 3 & 22 is 2.

Let's try another quick one, because it's so fun. What's 13 ^ 6 (13 eXclusiveOR 6)?

64

32

16

8

4

2

1

 

0

0

0

1

1

0

1

= 13 (because 1 + 0 + 4 + 8 = 13)

      

XOR

 

0

0

0

0

1

1

0

= 6 (because 0 + 2 + 4 = 6)


We can do this quickly now. Remember that exclusive or means that each operand must be a different value.

1: 1 XOR 0 = 1

2: 0 XOR 1 = 1

4: 1 XOR 1 = 0

8: 1 XOR 0 = 1

The answer is 1 + 2 + 0 + 8 = 11. 13 ^ 6 = 11.

Here is some Java code that prints out these different values just to prove it.

BooleanOps.java






public 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.

    Previous Section  < Day Day Up >  Next Section