Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The | operator

The | operator is the bitwise OR operator. It takes two numeric arguments, compares their bits, and sets the bits in the result that are set in either or both of the arguments. For example, let b1 be a byte with value 78 and b2 be a byte with the value -23. In binary, 78 is 01001110 and -23 is 11101001. Lay these values on top of each other as shown in Figure 2-6. The result, shown in the bottom row, is 11101111, that is -17.


Figure 2-6  78 | -23.

The bits that are equal to one in either 78 or -23 or both are equal to one in the result. All other bits are zero.

Of course, Java actually performs this calculation using 32-bit ints. Because the high-order three bytes of a positive int are just full of zeroes, the real result of 78 & -23 is 11111111111111111111111111101111. If either argument of | has a one bit in a particular position, that bit must be 1 in the result, regardless of the value of the bit in the second argument.

The | operator can also be used with two booleans: true | true is true, true | false is true, and false | false is false.

The AWT sometimes uses this to set a series of flags. If you have an item that has up to 32 boolean characteristics, then you can stuff all the values of those characteristics into an int.

For example, consider the java.awt.Font class. To create a new font, you use this constructor:

     public Font(String name, int style, int size)

The name is the name of the typeface, like Times or Arial. The size is the size of the font in points, such as 12 or 24. The style, however, is one of a special set of mnemonic constants. These constants are

     Font.BOLD = 1
     Font.PLAIN = 0
     Font.ITALIC = 2

You can pass one of these constants in the style argument of the Font constructor to get that style. However, what if you want a Font that is both bold and italic? Then, you pass Font.BOLD | Font.ITALIC. This means that the bold bit and the italic bit are both set in the style argument. Notice that Font.BOLD is 00000001 whereas Font.ITALIC is 00000010. Each bit in the number is a binary flag indicating the value of the binary characteristic; for example, is this or is this not bold? Other classes that use this scheme can have many more such constants, all of which are powers of two: 4, 8, 16, 32, 64, and so on. Each power of two is a 32-bit int with exactly one bit set and the rest unset.

As with &, | can also prevent the short-circuiting of expression evaluation. Consider the statement

     if ( isConditionOne() || isConditionTwo() ) doSomething();

If isConditionOne() returns true, then isConditionTwo() will not be called because Java knows the result will be true, regardless of the value of isConditionTwo(). To force isConditionTwo() to be called, use the bitwise | instead. That is

     if ( isConditionOne() | isConditionTwo() ) doSomething();

The truth value of ( isConditionOne() | isConditionTwo() ) is the same as the truth value of ( isConditionOne() || isConditionTwo() ), but now both methods are called.

The ^ operator

The ^ is the bitwise EXCLUSIVE-OR operator. The operator | does not behave like many people expect, based on its English meaning. Many people think the “A or B” is true if A is true and B is not true, or vice versa, but that “A or B” is not true if both A and B are true. ^ is the bitwise equivalent of this idea. The ^ operator takes two numeric arguments, compares their bits, and sets the bits in the result that are set in exactly one of the arguments.

Returning to the example where b1 is a byte with value 78 and b2 is a byte with the value -23, lay these values on top of each other as shown in Figure 2-7. The result, shown in the bottom row, is 10100111-89.


Figure 2-7  78 ^ -23.

The ^ operator can also be used with two booleans: true ^ true is false, true ^ false is true, and false ^ false is false.

The ~ operator

The ~ is the bitwise NOT or complement operator. It is unary; that is, it acts on a single number or boolean, and it flips all the bits in that value. As a result, all ones turn to zeroes and zeroes turn to ones. Figure 2-8 shows 78 and ~78.


Figure 2-8  78 ~ -23.

By the nature of two’s complement arithmetic, if b is an int or a long, then ~b equals -b - 1.

Assignment operators

The &=, |=, and ^= operators behave like their arithmetic cousins, *=, +=, -=, %= and /=. In other words, they combine the value on the left side of the operator with the value on the right side, and then assign it to the left side. For example:

     int a = 78;
     a &= -23;

This makes a equal to 76. |= and ^= behave similarly except they use bitwise OR and bitwise XOR respectively.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.