One, Java bit operation
1. Presentation method:
In the Java language, the binary number is represented by a complement, the highest bit is the sign bit, the sign bit for positive numbers is 0, and the negative is 1. The complement representation needs to meet the following requirements.
(l) The maximum number of positive digits is 0, and the remainder represents the value itself (binary number).
(2) For negative numbers, by the absolute value of the number of the complement of the bitwise negation, and then the whole number plus 1.
2. Bitwise operators
A bitwise operation expression consists of an operand and a bitwise operator that implements a bitwise operation on a binary number of integer types. Bitwise operators can be divided into logical operators (including ~, &, | and ^) and shift operators (including >>, <<, and >>>).
1) The left shift operator (<<) moves the operand to the left of the operator to the left by the number of digits specified to the right of the operator (in the low 0).
2) The "signed" Right Shift operator (>>) moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator.
The "signed" right shift operator uses "symbol extension": If the value is positive, insert 0 at the high, and if the value is negative, insert 1 at the high.
3) Java has also added an "unsigned" right shift operator (>>>) that uses the "0 extension": both positive and negative, inserting 0 in the high position. This operator is not in C or C + +.
4) If the Char,byte or short are shifted, they are automatically converted to an int before the shift is made.
Only the 5 lows on the right will be used. This prevents us from moving the impractical number of digits in an int number.
If a long value is processed, the last result is a long. Only the 6 lows on the right are used to prevent moving beyond the existing number of digits in the long value.
However, you may also encounter a problem when you perform an "unsigned" right shift. If you perform a right shift operation on a byte or short value, you may not get the correct result (Java 1.0 and Java 1.1 are particularly prominent).
They are automatically converted to the int type and are shifted right. But the "0 extension" will not happen, so in those cases you will get the result of-1.
The following points need to be taken into consideration when doing bit operations.
(1) The difference between >>> and >> is that the operand of the,>>> operator is high-up by 0 while performing the operation, while the operand of the >> operator is moved to the value of the original high.
(2) A right shift is equivalent to dividing by 2, moving left one (in the case of no overflow) equal to multiplying by 2, and the shift operation is faster than the multiplication operation.
(3) If the data length of the two operands of the bitwise logic operation is different, the return value should be a data type with a longer data length.
(4) Bitwise XOR or you can complete the interchange of two values without using a temporary variable, or you can flip the value of a particular bit of an integer number.
(5) Bitwise AND operations can be used to block specific bits or to take certain bits of a number.
(6) A bitwise OR operation can be used to set the value of a particular bit of an integral number to L.
3. Precedence of bitwise operators
~ has the highest priority, followed by <<, >> and >>>, and again is &, then ^, the lowest priority is |.
Two, bitwise XOR OR operator ^
The two values that participate in the operation, if two corresponding bits are the same, the result is 0, otherwise 1. namely: 0^0=0, 1^0=1, 0^1=1, 1^1=0
Example: 10100001^00010001=10110000
0^0=0,0^1=1 0 XOR or any number = any number
1^0=1,1^1=0 1 XOR or any number-any number taken against
Any number different or self = put yourself 0
(1) Bitwise XOR or can be used to make certain specific bits flip, such as the 2nd and 3rd bits of the logarithm 10100001 are flipped, the number and 00000110 can be bitwise XOR OR operation.
10100001^00000110=10100111//1010 0001 ^ 0x06 = 1010 0001 ^ 6
(2) by bitwise XOR, you can exchange two values without having to use a temporary variable. For example, a value of two integer, A, or a, can be implemented by the following statement:
a=10100001,b=00000110
A=a^b;//a=10100111
B=b^a;//b=10100001
A=a^b;//a=00000110
(3) XOR operator is characterized by: number a two XOR or the same number B (A=a^b^b) is still the original value A.
Third, Java In addition to the binary representation method:
Because the data is represented in the computer and eventually in binary form, sometimes binary is used to solve the problem more intuitively.
However, the binary number is too long. For example, an int type occupies 4 bytes and 32 bits. For example, 100, a binary number expressed in int type would be:
0000 0000 0000 0000 0110 0100
Faced with such a long number of thinking or operation, no one would like it. Therefore, c,c++, as well as Java, does not provide a way to write binary numbers directly in code.
An approach to the expression of eight decimal numbers
How to express an octal number? If this number is 876, we can conclude that it is not an octal number because it is not possible to have more than 7 Arabic numerals in octal numbers. But if this number is 123, is 567, or 12345670, then it is octal or 10 binary, it is possible.
So, a number, if you want to indicate that it uses octal, you must precede it with a 0, such as: 123 is decimal, but 0123 means octal. This is how the octal number is expressed.
Now, for the same number, such as 100, we can use the usual 10 in the code, for example, when the variable is initialized:
int a = 100;
We can also write this:
int a = 0144; 0144 is the octal 100, and how a 10 binary number is converted to 8 binary.
Remember, when you're using octal, you can't have the last 0 less. Otherwise the computer will be 10 binary. However, there is a place where octal numbers are used, but you cannot use Add 0, which is the "escape character" expression that we learned before to express a character.
How to express hexadecimal numbers
If a special writing form is not used, the 16 binary number is mixed with the 10 binary. Any number: 9876, you can not see it is 16 binary or 10 binary.
16 binary number must start with 0x. For example, 0x1 denotes a 16 binary number. and 1 represents a decimal. In addition, such as: 0xff,0xff,0x102a, and so on. The x is also not case sensitive. (Note: 0 in 0x is the number 0, not the letter O)
Here are some examples of usage:
int a = 0x100f;
int B = 0x70 + A;
The last point is important, the 10 binary number has positive and negative points, such as 12 means positive 12, and 12 for negative 12, but 8 and 16 binary can only use up to unsigned positive integers, if you are in the code:-078, or write: -0xf2, the compiler does not treat it as a negative number.
/* Bit arithmetic
* Java uses the complement to represent 2 decimal digits, the highest bit is the sign bit, the positive number is 0, the negative number is 1, the complement rules:
* Integer, the highest bit is 0, the rest is itself, such as +42 of the complement of 00101010
* Negative number, the highest bit is 1, the rest of the decision value is reversed, the last +1, that is, the complement of negative numbers;
* 42 of the complement is 11010110 (00101010 bitwise reverse 11010101 +1=11010110)
********
* Logical Operation *
********
A&b and the door [true True, false and true]
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0000
System.out.println (1 & 2);
Result is 0
A|b or Door [false for false, rest full true]
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0011
SYSTEM.OUT.PRINTLN (1 | 2);
Result is 3
~a non-gate [true False, False true]
0000 0000 0000 0000 0000 0000 0000 0001
=
1111 1111 1111 1111 1111 1111 1111 1110
System.out.println (to);
The result is:-2
A^b or gate: [same as false, different is true]
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0011
System.out.println (1^2);
Result is 3
********
* Shift Operation *
********
A>>b signed right shift; shift A to the right b bit; if positive, high fill 0, negative, high 1
0000 0000 0000 0000 0000 0000 0000 0011
>>1
=
0000 0000 0000 0000 0000 0000 0000 0001
System.out.println (3>>1);
The result is 1, and the result is the same as the 1 power of 3/2
A<<b signed left shift; shift A to the left B-position, if positive, high 0, negative, high-fill 1
0000 0000 0000 0000 0000 0000 0000 0011
<<2
0000 0000 0000 0000 0000 0000 0000 1100
System.out.println (3<<2);
The result is 12, the same as the power of 3*2 2
A>>>b unsigned right shift; shift A to the left B position, either positive or negative, the high is 0
0000 0000 0000 0000 0000 0000 0000 0011
>>1
=
0000 0000 0000 0000 0000 0000 0000 0001
System.out.println (3>>>1);
The result is 1, the same as the 1 power of 3/2
*/
System.out.println (3>>>1);
System.out.println ();
Java-Defined bitwise operations (bitwise operators) operate directly on bits of the integer type, which include Long,int,short,char,and byte.
Operator results
~ Bitwise NON (not) (unary operation)
& Bitwise AND (and)
| bitwise OR (OR)
^ Bitwise XOR (XOR)
>> Right Shift
>>> right shift, left empty bits filled with 0
Operator results
<< left Shift
& = Bitwise AND Assignment
|= Bitwise OR Assignment
^= Bitwise XOR or Assignment
>> = Right Shift Assignment
>>> = Right Shift assignment, left empty bit filled with 0
<< = Left Shift Assignment
Since the bitwise operator operates on a bitwise integer range, it is important to understand what such an operation would do to a value. Specifically, it is useful to know how Java stores integer values and how to represent negative numbers. So before we go on, let's briefly summarize the two topics.
All integer types are represented by the change in binary digits and their width. For example, the binary code for byte-value 42 is 00101010, where each position represents 2 of the secondary, and the rightmost bit begins with 20. The next position to the left will be 21, or 2, then left is 22, or 4, then 8,16,32, and so on. Thus 42 the value of 1,3,5 at its position is 1 (starting from the right with 0); So 42 is the 21+23+25 and the 2+8+32.
All integer types (except the char type) are signed integers. This means that they can represent both positive and negative numbers. Java uses the code known as the 2 complement (two ' s complement) to represent negative numbers by inverting the binary code of its corresponding positive number (turning 1 to 0, 0 to 1), and then adding 1 to its result. For example, -42 is by inverting the bits of the 42 binary code, which is 11010101 for 00101010 and then 1 for 11010110, or 42. To decode a negative number, first reverse all of its bits, then add 1. For example-42, or 11010110 after inversion is 00101001, or 41, and then add 1, so that gets 42.
If you take into account the zero crossing problem, you can easily understand why Java (and most other languages) use the 2 complement. The value of the byte type is assumed to be 00000000 representative. Its complement is simply to reverse each of its bits, which generates 11111111, which represents negative 0. But the problem is that negative 0 is not valid in integer math. To solve the problem of negative zeros, add 1 to the value of the negative value when using 2 's complement. That is, minus 11,111,111 plus 1 is 100000000. But this makes 1 bits too left to return to the value of byte type, so it is stipulated that 0 and 0 are represented the same way,-1 is decoded to 11111111. Although we used the byte type value in this example, the same basic principle applies to all Java integer types.
Because Java uses 2 of the complement to store negative numbers, and because all integers in Java are signed, applying bit operators can easily achieve unexpected results. For example, regardless of how you intend, Java uses a high to represent a negative number. To avoid this nasty accident, keep in mind that regardless of the order of the highs, it determines the sign of an integer.
4.2.1-bit logical operators
The bitwise logical operators are "with" (and), "or" (or), "XOR", "Non", "&", "|", "^", "~" respectively, and the 4-3 table shows the result of each bit logical operation. Before continuing with the discussion, remember that the bitwise operator is applied to each individual bit within each operand.
Results of table 4-3-bit logical operators
A 0 1 0 1 B 0 0 1 1 A | b 0 1 1 1 A & B 0 0 0 1 A ^ B 0 1 1 0 ~a 1 0 1 0
Bitwise NON (NOT)
A bitwise non is also called a complement, and a unary operator not "~" is the negation of each bit of its operand. For example, the number 42, its binary code is:
00101010
After a bitwise non-operation becomes
11010101
Bitwise VS (and)
Bitwise AND Operator "&", if all two operands are 1, the result is 1. In other cases, the result is zero. Look at the following example:
00101010 &00001111 15
00001010 10
bitwise OR (OR)
The bitwise OR operator "|", any one operand is 1, the result is 1. As shown in the following example:
00101010 42 | 00001111 15
00101111 47
Bitwise XOR OR (XOR)
Bitwise XOR Operator "^", only if the bits in both comparisons are not at the same time its result is 1. Otherwise, the result is zero. The following example shows the effect of the "^" operator. This example also shows a useful property of the XOR operator. Note that the second operand has a bit of number 1, and 42 corresponds to how the corresponding bit of the binary code is converted. The second operand has a bit of number 0, and the number of the first operand corresponds to the digit. When you perform bitwise operations on certain types, you will see the usefulness of this property.
00101010 42 ^ 00001111 15
00100101 37
The application of bit logical operators
The following example illustrates the bitwise logical operators:
Demonstrate the bitwise logical operators.
Class Bitlogic {
public static void Main (String args[]) {
String binary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; 0 + 2 + 1 or 0011 in binary
int B = 6; 4 + 2 + 0 or 0110 in binary
int c = A | b
int d = A & B;
int e = a ^ b;
int f = (~a & B) | (A & ~b);
int g = ~a & 0x0f;
System.out.println ("a =" + Binary[a]);
System.out.println ("b =" + binary);
System.out.println ("a|b =" + binary[c]);
System.out.println ("a&b =" + binary[d]);
System.out.println ("a^b =" + binary[e]);
System.out.println ("~a&b|a&~b =" + binary[f]);
System.out.println ("~a =" + binary[g]);
}
}
In this case, the combination of the variable A and b corresponding bits represents all 4 combinations of binary numbers: 0-0,0-1,1-0, and 1-1. | The operator and the "&" operator respectively calculate the value of the variable C and the variable d for each corresponding bit of the variable A and B. The assignment of variables E and F illustrates the function of the "^" operator. The string array binary represents the binary value that corresponds to 0 to 15. In this example, the order of the elements in the array shows the binary code of the variable's corresponding value. The array is constructed because the binary code corresponding to the value n of the variable can be stored correctly in the corresponding element of the array binary[n]. For example, the value of variable A is 3, and its binary code is stored accordingly in the array element binary[3]. The value of the ~a and the numeric 0x0f (corresponding to the binary 0000 1111) are bitwise and calculated to reduce the value of ~a, guaranteeing that the result of the variable g is less than 16. Therefore, the running results of the program can be represented by the elements corresponding to the binary array. The output of the program is as follows:
A = 0011 b = 0110 a|b = 0111 A&b = 0010 A^b = 0101 ~a&b|a&~b = 0101 ~a = 1100
4.2.2 Left shift operator
The left shift operator << causes all bits of the specified value to shift to the left of the prescribed number of times. Its general format is as follows:
Value << num
Here, num Specifies the number of bits to shift the value to move. That is, the left shift operator << causes all bits of the specified value to be left to the NUM bit. Each left shifts one bit, and the higher-order bits are removed (and discarded) and filled to the right with 0. This means that when the left-hand operand is of type int, the 31st bit of each 1-bit shift is moved out and discarded, and when the left-hand operand is a long, the 63rd bit of each 1-bit move is removed and discarded.
You must be careful when you shift operations on the values of byte and short types. Because you know that Java will automatically expand these types to int when evaluating an expression, and the value of the expression is int. The result of the shift operation on the values of byte and short is the int type, and if the left shift is no more than 31 bits, the original values for each of you will not be discarded. However, if you shift the value of a negative byte or short type, it is expanded to int, and its symbol is expanded. Thus, the high of the integer value result is filled by 1. Therefore, in order to get the right result, you will have to abandon the high level of results. The simplest way to do this is to convert the result to a byte type. The following procedure illustrates this point:
Left shifting a byte value.
Class Byteshift {
public static void Main (String args[]) {
byte a = n, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println ("Original value of a:" + a);
System.out.println ("I and B:" + i + "" + B);
}
}
The output generated by the program is shown below:
Original Value of a:64
I and b:256 0
The dependent variable A in the assignment expression, it is enlarged to int, 64 (0100 0000) is shifted left two times the generated value 256 (10000 0000) is assigned to the variable i. However, after the left shift, the only 1 of the variable B is moved out and the low level is all 0, so the value of B also becomes 0.
Since each left shift can double the original operand, programmers often use this method for fast multiplication of 2. But you have to be careful, if you move 1 into higher-order bits (31 or 63-bit), the value will become negative. The following procedure illustrates this point:
Left shifting as a quick-to-multiply by 2.
Class Multbytwo {
public static void Main (String args[]) {
int i;
int num = 0xFFFFFFE;
for (i=0; i<4; i++) {
num = num << 1;
SYSTEM.OUT.PRINTLN (num);
}
}
Here, num Specifies the number of bits to shift the value to move. That is, the left shift operator << causes all bits of the specified value to be left to the NUM bit. Each left shifts one bit, and the higher-order bits are removed (and discarded) and filled to the right with 0. This means that when the left-hand operand is of type int, the 31st bit of each 1-bit shift is moved out and discarded, and when the left-hand operand is a long, the 63rd bit of each 1-bit move is removed and discarded.
You must be careful when you shift operations on the values of byte and short types. Because you know that Java will automatically expand these types to int when evaluating an expression, and the value of the expression is int. The result of the shift operation on the values of byte and short is the int type, and if the left shift is no more than 31 bits, the original values for each of you will not be discarded. However, if you shift the value of a negative byte or short type, it is expanded to int, and its symbol is expanded. Thus, the high of the integer value result is filled by 1. Therefore, in order to get the right result, you will have to abandon the high level of results. The simplest way to do this is to convert the result to a byte type. The following procedure illustrates this point:
Left shifting a byte value.
Class Byteshift {
public static void Main (String args[]) {
byte a = n, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println ("Original value of a:" + a);
System.out.println ("I and B:" + i + "" + B);
}
}
The output generated by the program is shown below:
Original Value of a:64
I and b:256 0
The dependent variable A in the assignment expression, it is enlarged to int, 64 (0100 0000) is shifted left two times the generated value 256 (10000 0000) is assigned to the variable i. However, after the left shift, the only 1 of the variable B is moved out and the low level is all 0, so the value of B also becomes 0.
Since each left shift can double the original operand, programmers often use this method for fast multiplication of 2. But you have to be careful, if you move 1 into higher-order bits (31 or 63-bit), the value will become negative. The following procedure illustrates this point:
Left shifting as a quick-to-multiply by 2.
Class Multbytwo {
public static void Main (String args[]) {
int i;
int num = 0xFFFFFFE;
for (i=0; i<4; i++) {
num = num << 1;
SYSTEM.OUT.PRINTLN (num);
}
}
}
The output of the program is as follows:
536870908
1073741816
2147483632
-32
The initial value is carefully selected so that when you move 4 bits to the left, it produces-32. As you can see, when 1 is moved into 31 digits, the number is interpreted as a negative value.
4.2.3 Right Shift operator
Right-shift operator >> causes all bits of the specified value to move to the right by the required number of times. Its general format is as follows:
Value >> num
Here, num Specifies the number of bits to shift the value to move. That is, right-shift operator >> make all bits of the specified value move the NUM bit right. The following program fragment shifts the value 32 right 2 times and assigns the result 8 to the variable A:
int a = 32;
A = a >> 2; A now contains 8
When some bits in a value are "moved out", the values of those bits are discarded. For example, the following program fragment shifts 35 Right 2 times, its 2 lows are moved out of the discard, and the result 8 is assigned to variable a:
int a = 35;
A = a >> 2; A still contains 8
A binary representation of the process allows you to see more clearly the program's running process:
00100011 35
>> 2
00001000 8
Moving the value every right time is equivalent to dividing the value by 2 and discarding the remainder. You can use this feature to divide an integer fast by 2. Of course, you must make sure that you do not remove any of the original numbers.
When you move to the right, the top bit (the leftmost bit) is removed and is supplemented by the highest number of previous digits. For example, if the value to be removed is a negative number, each right shift is 1 on the left, and if the value to be moved is a positive number, each right shift is 0 on the left, which is called symbol bit extension (reserved sign bit) (sign extension), which is used to hold negative numbers when moving right. For example, –8 >> 1 is a –4, which is represented by binary as follows:
11111000–8 >>1 11111100–4
One interesting question to note is that since the sign bit extension (reserved sign bit) will be 1 at the high level each time, 1 the result of the right shift is always –1. Sometimes you don't want to keep the symbols when you move right. For example, the following example converts the value of a byte type to 16
Binary representation. Note that the right-shifted value is bitwise AND operation with 0x0f so that any sign bit extension can be discarded so that the resulting value can be used as the subscript for the definition array, resulting in the hexadecimal character represented by the corresponding array element.
Masking sign extension.
Class HexByte {
static public void Main (String args[]) {
Char hex[] = {
' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ',
' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '
};
byte B = (byte) 0xf1;
System.out.println ("B = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);}}
The output of the program is as follows:
b = 0xf1
4.2.4 Unsigned Right Shift
As we just saw, each right-shift,>> operator always automatically complements its highest bit with the content of its previous highest bit. This preserves the symbol of the original value. But sometimes it's not what we want. For example, if the operand of your shift operation is not a numeric value, you do not want the symbol bit extension (reserved sign bit). This is quite common when you are working with pixel values or graphics. In this case, regardless of the initial value of the operand, you want the shift to always be in the high (leftmost) 0. This is what people call an unsigned move (unsigned shift). You can then use the Java unsigned right-shift operator >>>, which always complements 0 on the left.
The following procedure section describes the unsigned right shift operator >>>. In this case, the variable A is assigned a value of-1, and the binary representation is that 32 bits are all 1. This value is then shifted to the left by an unsigned 24-bit, which, of course, ignores the sign bit extension and always complements 0 on its right. The resulting value of 255 is assigned to variable a.
int a =-1; A = a >>> 24;
This operation is further explained in binary form:
11111111 11111111 11111111 11111111 INT-1 binary code >>> 24 unsigned Right shift 24 bit 00000000 00000000 00000000 11111111 binary code of type int 255
Because the unsigned right-shift operator >>> only makes sense for 32-bit and 64-bit values, it's not as useful as you might think. Because you have to remember that a value that is too small in an expression is always automatically expanded to be of type int. This means that the symbol bit expansion and movement always occurs in 32 bits instead of 8 bits or 16 bits. Thus, it is not possible to do an unsigned move on the 7th bit of a byte of type 0, since the expanded 32-bit value is manipulated when the operation is actually moved. This is illustrated in the following example:
Unsigned shifting a byte value.
Class Byteushift {
static public void Main (String args[]) {
Binary representation. Note that the right-shifted value is bitwise AND operation with 0x0f so that any sign bit extension can be discarded so that the resulting value can be used as the subscript for the definition array, resulting in the hexadecimal character represented by the corresponding array element.
Masking sign extension.
Class HexByte {
static public void Main (String args[]) {
Char hex[] = {
' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ',
' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '
};
byte B = (byte) 0xf1;
System.out.println ("B = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);}}
The output of the program is as follows:
b = 0xf1
4.2.4 Unsigned Right Shift
As we just saw, each right-shift,>> operator always automatically complements its highest bit with the content of its previous highest bit. This preserves the symbol of the original value. But sometimes it's not what we want. For example, if the operand of your shift operation is not a numeric value, you do not want the symbol bit extension (reserved sign bit). This is quite common when you are working with pixel values or graphics. In this case, regardless of the initial value of the operand, you want the shift to always be in the high (leftmost) 0. This is what people call an unsigned move (unsigned shift). You can then use the Java unsigned right-shift operator >>>, which always complements 0 on the left.
The following procedure section describes the unsigned right shift operator >>>. In this case, the variable A is assigned a value of-1, and the binary representation is that 32 bits are all 1. This value is then shifted to the left by an unsigned 24-bit, which, of course, ignores the sign bit extension and always complements 0 on its right. The resulting value of 255 is assigned to variable a.
int a =-1; A = a >>> 24;
This operation is further explained in binary form:
11111111 11111111 11111111 11111111 INT-1 binary code >>> 24 unsigned Right shift 24 bit 00000000 00000000 00000000 11111111 binary code of type int 255
Because the unsigned right-shift operator >>> only makes sense for 32-bit and 64-bit values, it's not as useful as you might think. Because you have to remember that a value that is too small in an expression is always automatically expanded to be of type int. This means that the symbol bit expansion and movement always occurs in 32 bits instead of 8 bits or 16 bits. Thus, it is not possible to do an unsigned move on the 7th bit of a byte of type 0, since the expanded 32-bit value is manipulated when the operation is actually moved. This is illustrated in the following example:
Unsigned shifting a byte value.
Class Byteushift {
static public void Main (String args[]) {
int b = 2;
int c = 3;
A |= 4;
b >>= 1;
C <<= 1;
a ^= C;
System.out.println ("a =" + a);
System.out.println ("b =" + B);
System.out.println ("c =" + C);
}
}
The output of the program is as follows:
A = 3
b = 1
c = 6
Source: http://blog.csdn.net/dingxy/archive/2009/04/30/4140149.aspx
Java bit Operations