The
shift operator is oriented to an Operation object that is also a binary "bit". You can use them individually to handle integer types (one of the main types). The left shift operator (<<) is able to move the operand on the left side of the operator to the right of the specified number of digits on the right-hand side of the operator (0 in low). The signed right shift operator (>>) assigns the Operation object to the left of the operator to the right of the specified number of digits to the right-hand operator. The "signed" right shift operator uses the symbol extension: If the value is positive, insert 0 at the high level, or 1 in the high position if the value is negative. Java also adds a "unsigned" right shift operator (>>>), which uses "0 extensions": Inserts 0 at high levels, either positive or negative. This operator is not in C or C + +.
If Char,byte or short are shifted, they are automatically converted to an int before the shift occurs. Only 5 lows to the right are used. This prevents us from moving an unrealistic number of digits in an int. If a long value is processed, the resulting result is long. Only the 6 lows on the right are used at this point to prevent moving more than the number of bits in the Long value. However, you may also encounter a problem when you make an unsigned right shift. The right shift operation on the byte or short value may not be the correct result (Java 1.0 and Java 1.1 are particularly prominent). They are automatically converted to type int and are shifted to the right. But "0 extensions" don't happen, so in those cases you get the result of 1. You can use the following example to detect your own implementation:
: Urshift.java
//Test of unsigned Right shift public
class Urshift {public
static void Main (string[] args) {
int i =-1;
I >>>=;
System.out.println (i);
Long L =-1;
l >>>=;
System.out.println (l);
Short S =-1;
s >>>=;
System.out.println (s);
byte B =-1;
b >>>=;
System.out.println (b);
}
///:~
The shift can be used in combination with an equal sign (<<= or >>= or >>>=). At this point, the value on the left side of the operator moves the number of digits specified by the value on the right, and then assigns the resulting result back to the value on the left.
Here's an example of how to apply all the operators that involve a bitwise operation and their effects:
: Bitmanipulation.java//Using The bitwise operators import java.util.*;
public class Bitmanipulation {public static void main (string[] args) {Random rand = new Random ();
int i = Rand.nextint ();
Int J = Rand.nextint ();
Pbinint ("-1",-1);
Pbinint ("+1", + 1);
int maxpos = 2147483647;
Pbinint ("Maxpos", Maxpos);
int Maxneg =-2147483648;
Pbinint ("Maxneg", Maxneg);
Pbinint ("I", I);
Pbinint ("~i", ~i);
Pbinint ("I",-i);
Pbinint ("J", J);
Pbinint ("I & J", I & J); Pbinint ("I | J ", I |
j);
Pbinint ("I ^ j", I ^ j);
Pbinint ("I << 5", I << 5);
Pbinint ("I >> 5", I >> 5);
Pbinint ("(~i) >> 5", (~i) >> 5);
Pbinint ("I >>> 5", I >>> 5);
Pbinint ("(~i) >>> 5", (~i) >>> 5);
Long L = Rand.nextlong ();
Long m = Rand.nextlong ();
Pbinlong (" -1l", -1l);
Pbinlong ("+1l", +1l);
Long ll = 9223372036854775807L; Pbinlong ("MaxpOs ", LL);
Long Lln = -9223372036854775808l;
Pbinlong ("Maxneg", Lln);
Pbinlong ("L", l);
Pbinlong ("~l", ~l);
Pbinlong ("L",-l);
Pbinlong ("M", m);
Pbinlong ("L & M", L & M); Pbinlong ("l | M ", l |
m);
Pbinlong ("l ^ m", l ^ m);
Pbinlong ("L << 5", L << 5);
Pbinlong ("L >> 5", L >> 5);
Pbinlong ("(~l) >> 5", (~L) >> 5);
Pbinlong ("L >>> 5", L >>> 5);
Pbinlong ("(~l) >>> 5", (~L) >>> 5);
} static void Pbinint (String s, int i) {System.out.println (s +), int: "+ i +", binary: ");
System.out.print ("");
for (int j =; J >=0; j--) if (((1 << j) & I)!= 0) System.out.print ("1");
else System.out.print ("0");
System.out.println ();
static void Pbinlong (String s, long L) {System.out.println (S +), long: "+ L +", Binary: ");
System.out.print (""); for (int i = i >=0; i--) if (((1L << i) & L)!= 0) System.out.print ("1");
else System.out.print ("0");
System.out.println (); }
} ///:~
Two methods were called at the end of the
Program: Pbinint () and Pbinlong (). They manipulate an int and a long value and output in a binary format with a brief explanatory text. At present, they can temporarily ignore their specific implementation scenarios.
Note that the use of System.out.print () is not System.out.println (). The print () method does not produce a new row to list multiple information in the same row.
In addition to showing the effects of all bitwise operators on int and long, this example shows the minimum, maximum, +1, and -1 values of int and long, so that you can appreciate their situation. Note that the high position represents positive sign: 0 is positive and 1 is negative. The output of the INT section is listed below:
-1, int:-1, binary:11111111111111111111111111111111 +1, int:1, binary:00000000000000000000000000000001 Maxpos , int:2147483647, binary:01111111111111111111111111111111 maxneg, int: -2147483648, binary:10000000000000000000 000000000000 I, int:59081716, binary:00000011100001011000001111110100 ~i, int: -59081717, binary:11111100011110 100111110000001011-i, int: -59081716, binary:11111100011110100111110000001100 J, int:198850956, binary:0000101 1110110100011100110001100 I & J, int:58720644, binary:00000011100000000000000110000100 I | J, int:199212028, binary:00001011110111111011101111111100 i ^ j, int:140491384, binary:00001000010111111011101
001111000 I << 5, int:1890614912, binary:01110000101100000111111010000000 i >> 5, int:1846303, binary: 00000000000111000010110000011111 (~i) >> 5, int: -1846304, binary:11111111111000111101001111100000 i >&G T;> 5, int:1846303, binary:00000000000111000010110000011111 (~i) >>> 5, int:132371424, binary:00000111111000111101001111100000
The binary form of a number is expressed as "a complement of signed 2".