Java Reference
In-Depth Information
The result of the shift right operation is stored in
packed
, so ANDing
mask
with
packed
extracts the next
letter. Extraction of the next two letters is achieved by repeating exactly the same process of shifting and
then ANDing with
mask
. From the output you can see that it all works as it should.
Methods for Bitwise Operations
In addition to the basic Java language facilities for operations on integers at the bit level, you also have some
methods available in library classes that provide you with a few extra capabilities. I won't go into great de-
tail on these as they're rather specialized, but I'll outline the methods and explain what they do so you are
aware of them.
The methods that implement bitwise operations are defined in the
Integer
and
Long
classes in the
java.lang
package. The methods in the
Integer
class apply to values of type
int
, and the methods in the
Long
class apply to values of type
long
. Both classes define the static methods for bitwise operations, shown
METHOD DESCRIPTION
bitCount(arg)
Returns the number of 1 bits in the binary integer that you supply as
arg
. The count is
returned as a value of type
int
.
highestOneBit(arg)
Returns an integer with a single 1 bit in the position corresponding to the leftmost 1
bit in
arg
. The value is returned as the same type as
arg
.
lowestOneBit(arg)
Returns an integer with a single 1 bit in the position corresponding to the rightmost 1
bit in
arg
. The value is returned as the same type as
arg
.
numberOfLeadingZeros(arg)
Returns the number of 0 bits preceding the leftmost 1 bit in
arg
. The value is returned
as type
int
. If
arg
is zero then the method returns the total number of bits in
arg
,
which is 32 for type
int
and 64 for type
long
.
numberOfTrailingZeros(arg)
Returns the number of 0 bits following the rightmost 1 bit in
arg
. The value is re-
turned as type
int
. If
arg
is zero then the method returns the total number of bits in
arg
, which is 32 for type
int
and 64 for type
long
.
reverse(arg)
Returns the value that is obtained by reversing the order of bits in
arg
. The value is
returned as the same type as
arg
.
rotateLeft(arg, distance)
Returns the value obtained by rotating the bits in
arg
left by
distance
bits positions,
where
distance
is a value of type
int
. Rotating left means that bits shifted out on the
left are shifted into vacated bit positions on the right. The value is returned as the
same type as
arg
.
rotateRight(arg, distance)
Returns the value obtained by rotating the bits in
arg
right by
distance
bits posi-
tions, where
distance
is a value of type
int
. Rotating right means that bits shifted
out on the right are shifted into vacated bit positions on the left. The value is returned
as the same type as
arg
.
The return value is of the same type as the argument in each case where the result is a transformed version
of the argument. Where it is simply a count, the value returned is of type
int
.
If you think about what you would need to do yourself to implement what these methods do, you'll realize
they can save a lot of effort. To count how many 1 bits there are in an integer, you would need to work
through each of the bits in a loop checking for a 1 bit in each case. With the
bitCount()
method, you get