System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə29/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   25   26   27   28   29   30   31   32   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

value << num­bits
value >> num­bits
value >>> num­bits


Here, value is the value being shifted by the number of bit positions specified by num­
bits.
Each left shift causes all bits within the specified value to be shifted left one position
and a 0 bit to be brought in on the right. Each right shift shifts all bits to the right one
position and preserves the sign bit. As you may know, negative numbers are usually
represented by setting the high­order bit of an integer value to 1, and this is the
approach used by Java. Thus, if the value being shifted is negative, each right shift
brings in a 1 on the left. If the value is positive, each right shift brings in a 0 on the left.
In addition to the sign bit, there is something else to be aware of when right shifting.
Java uses two’s complement to represent negative values. In this approach negative
values are stored by first reversing the bits in the value and then adding 1. Thus, the
byte value for –1 in binary is 1111 1111. Right shifting this value will always produce –1!
If you don’t want to preserve the sign bit when shifting right, you can use an unsigned
right shift (>>>), which always brings in a 0 on the left. For this reason, the >>> is also
called the zero­fill right shift. You will use the unsigned right shift when shifting bit
patterns, such as status codes, that do not represent integers.
For all of the shifts, the bits shifted out are lost. Thus, a shift is not a rotate, and there is
no way to retrieve a bit that has been shifted out.
Shown next is a program that graphically illustrates the effect of a left and right shift.
Here, an integer is given an initial value of 1, which means that its low­order bit is set.
Then, a series of eight shifts are performed on the integer. After each shift, the lower 8
bits of the value are shown. The process is then repeated, except that a 1 is put in the
8th bit position, and right shifts are performed.


The output from the program is shown here:


You need to be careful when shifting byte and short values because Java will
automatically promote these types to int when evaluating an expression. For example,
if you right shift a byte value, it will first be promoted to int and then shifted. The
result of the shift will also be of type int. Often this conversion is of no consequence.
However, if you shift a negative byte or short value, it will be sign­extended when it is
promoted to int. Thus, the high­order bits of the resulting integer value will be filled
with ones. This is fine when performing a normal right shift. But when you perform a
zero­fill right shift, there are 24 ones to be shifted before the byte value begins to see
zeros.
Bitwise Shorthand Assignments
All of the binary bitwise operators have a shorthand form that combines an assignment
with the bitwise operation. For example, the following two statements both assign to x
the outcome of an XOR of x with the value 127.
Ask the Expert
Q
: Since binary is based on powers of two, can the shift operators be
used as a shortcut for multiplying or dividing an integer by two?


A
: Yes. The bitwise shift operators can be used to perform very fast multiplication
or division by two. A shift left doubles a value. A shift right halves it.
Try This 5­3
A ShowBits Class
This project creates a class called ShowBits that enables you to display in binary the
bit pattern for any integer value. Such a class can be quite useful in programming. For
example, if you are debugging device­driver code, then being able to monitor the data
stream in binary is often a benefit.
1. Create a file called ShowBitsDemo.java.
2. Begin the ShowBits class as shown here:
ShowBits creates objects that display a specified number of bits. For example, to
create an object that will display the low­order 8 bits of some value, use
The number of bits to display is stored in numbits.
3. To actually display the bit pattern, ShowBits provides the method show( ), which
is shown here:


Notice that show( ) specifies one long parameter. This does not mean that you always
have to pass show( ) a long value, however. Because of Java’s automatic type
promotions, any integer type can be passed to show( ). The number of bits displayed
is determined by the value stored in numbits. After each group of 8 bits, show( )
outputs a space. This makes it easier to read the binary values of long bit patterns.
4. The ShowBitsDemo program is shown here:



5. The output from ShowBitsDemo is shown here:
THE ? OPERATOR
One of Java’s most fascinating operators is the ?. The ? operator is often used to replace
if­else statements of this general form:
if (condition)

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   25   26   27   28   29   30   31   32   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə