Bitwise Operators

   

Depending on the types of programs you write, the bitwise operators are either very important or completely unimportant to you. If you write typical business applications, you will find little need to employ these operators. However, if you develop engineering applications that interface with hardware systems or binary files, the opposite is likely true. When you need a bitwise operator, it is rarely the case that you can substitute any other operation to easily reproduce the same results. However, most of the work you do probably won't put you in this position in the first place.

Bitwise operators work at the fundamental level of how values are stored in a computer. This is probably remedial, but, for completeness, it should be covered here. Computers store everything using values stored as sequences of on and off, known as bits, which are most often translated to the binary digits 1 and 0. For example, 32 of these 1s and 0s are required to store a variable of type int. Most of the time, you will manage the value held by an int variable using assignment statements and the various unary and arithmetic operators. Bitwise operators give you the additional option of manipulating the bits used to store a variable directly.

Consider a simple example using bytes. A byte comprises 8 bits of memory. Each of the 8 bits can have the value of 0 or 1, and the value of the whole quantity is determined by using base-2 arithmetic, meaning that the rightmost bit represents a value of 0 or 1; the next bit represents the value of 0 or 2; the next represents the value 0 or 4; and so on. The overall value represented by a sequence of bits is just the sum of these individual values. Table 5.3 shows the binary representation of several numbers as an example.

Table 5.3. Some Base-10 Values and Their Base-2 Equivalents
Base-10 Value 128 (27) 64 (26) 32 (25) 16 (24) 8 (23) 4 (22) 2 (21) 1 (20)
17 1 1
63 1 1 1 1 1 1
75 1 1 1 1
131 1 1 1

To find the base-10, or decimal, value of a number in Table 5.3, you add the numbers at the top of each column that contains a 1 for a given row. These columns represent the bits that are set in the number's base-2, or binary, representation. For instance, the first row shows that the binary number 00010001 is equivalent to

 16+1 = 17 

Twos -Complement

The numeric quantities in Table 5.3 are all positive integers, and that is intentional. Negative numbers are a little more difficult to represent. For any integer quantity in Java, except a char, the leftmost bit is reserved for the sign bit. If the sign bit is 1, the value is negative. The rest of the bits in a negative number are also interpreted a little differently using what is known as twos-complement. When using twos-complement, a negative number is represented by inverting each bit of the binary representation of the number's magnitude and adding 1. You can see this illustrated in these examples of binary representations:

 1 = 0...0001 -1 = 1...1111  2 = 0...0010 -2 = 1...1110 

The addition of 1 to the inverted bit sequence might sound arbitrary, but it is significant because it avoids having two representations for zero. Without this adjustment, both all 0s and all 1s would be interpreted as zero (positive zero and negative zero, strictly speaking). Instead, all 0s represents zero and all 1s represents “1 in twos-complement.

Now that you've seen examples of bit representations, the results of applying the bitwise operators are more intuitive. The bitwise operators perform the logical operations of AND, OR, Exclusive OR (sometimes called XOR), and Complement (or NOT) on each bit in turn . The operators are

  • Bitwise AND: &

  • Bitwise OR:

  • Bitwise Exclusive OR: ^

  • Bitwise Complement: ~

The &, , and ^ operators are binary operators and ~ is a unary operator. To determine the result of applying a bitwise operator, it is necessary to view each of the operands in its base-2 representation (a sequence of 1s and 0s). For each bit position in the operands, a bitwise operator sets the corresponding bit in the result based on what is known as the operator's truth table. Each of the operators has a different truth table as shown in the following:

First Value (a) Second Value (b) Result (a & b)
1
1
1 1 1
First Value (a) Second Value (b) Result (a b)
1 1
First Value (a) Second Value (b) Result (a b)
1 1
1 1 1
First Value (a) Second Value (b) Result (a ^ b)
1 1
1 1
1 1
Value (a) Result (~a)  
1  
1  

The operands of the bitwise operators can be any integer type. Booleans can also be operands to each of the bitwise operators with the exception of the complement operator. When you apply &, , or ^ to two booleans, the result is the same as that given in the preceding truth table if you substitute a value of true for each 1 and false for each 0.

If integer operands are used, each operand is promoted to at least an int and the result is an int. If one operand is a long, the other is promoted to a long if necessary, and the result is returned as a long. These rules are true for any of the operators applied to integers. If a boolean operand is used, both operands must be boolean and the result is a boolean.

Table 5.4 shows the results of applying each of the bitwise operators to two arbitrary values. First, you see the binary representations of the two decimal numbers 11309 and 798, and then the resulting bit sequences after the various operators are applied.

Table 5.4. Bitwise Operation Examples
Expression Binary Representation
11309 0010 1100 0010 1101
798 0000 0011 0001 1110
11309 & 798 0000 0000 0000 1100
11309 798 0010 1111 0011 1111
11309 ^ 798 0010 1111 0011 0011
~798 1111 1100 1110 0001

Similar to the arithmetic assignment operators, the bitwise operators are also supported as shorthand assignment operators. You can use &=, =, or ^= to perform a bitwise operation and assign the result back to the left operand.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net