Bitwise operators and operator priorities in Java

Source: Internet
Author: User
Bitwise operators and operator Priority 1-bit operators in Java
There are three shift operators, left shift <, right shift> and unsigned shift>. Left shift <add 0 at the low position. Right Shift> If the value is regular, insert 0 at the high position, and if the value is negative, insert 1 at the high position. Unsigned right shift >>> both positive and negative values insert 0 at the high position.
Non-operator ~
& Execute Boolean algebra on the corresponding bits in the two integer operands. If both bits are 1, 1 is output; otherwise, 0 is output.
^ Boolean algebra is executed for the corresponding bits in two integer operands. The two bits are equal to 0, ranging from 1.
| Boolean algebra is executed for the corresponding bits in the two integer operands. If both bits are 0, 0 is output; otherwise, 1 is output.
For example:
Which of the following operations is correct:
A. 1010 0000 0000 0000 0000 0000 0000 0000> 4
Gives 0000 1010 0000 0000 0000 0000 0000 0000
B. 1010 0000 0000 0000 0000 0000 0000 0000> 4
Gives 1111 1010 0000 0000 0000 0000 0000 0000
C. 1010 0000 0000 0000 0000 0000 0000 0000> 4
Gives 0000 1010 0000 0000 0000 0000 0000 0000
D. 1010 0000 0000 0000 0000 0000 0000 0000> 4
Gives 1111 1010 0000 0000 0000 0000 0000 0000
Option: B C

Which of the following operations is correct:
A. 0000 0100 0000 0000 0000 0000 0000 0000 <5
Gives 1000 0000 0000 0000 0000 0000 0000 0000
B. 0000 0100 0000 0000 0000 0000 0000 0000 <5
Gives 1111 1100 0000 0000 0000 0000 0000 0000
C. 1100 0000 0000 0000 0000 0000 0000 0000> 5
Gives 1111 1110 0000 0000 0000 0000 0000 0000
D. 1100 0000 0000 0000 0000 0000 0000 0000> 5
Gives 0000 0110 0000 0000 0000 0000 0000 0000
Select a C.

Given:
1. Public class test (
2. Public static void main (string ARGs []) (
3. system. Out. printin (6 ^ 3 );
4 .)
5 .)
What is the output
Ans: 5

The two-digit operator returns a value, not a Boolean value.
For example:
If (5 & 7> 0 & 5 | 2)
System. Out. println ("true ");
Show: Compilation Error

3. For an integer, the right operand should be smaller than 32. For a long integer, the right operand should be smaller than 64. If the right operand is greater than the specified number of digits, no error will be reported during compilation. Take the modulo of the right operand for the shift operation.
For example:
Class Test
{
Public static void main (string ARGs [])
{
Int x = 16384;
System. Out. println (x> 33 );
}
}
Display: 8192

4. Note that the bitwise operation is automatically converted to the int type. After the conversion, the length of the right operand is acceptable. During bitwise operations, the short and byte values are always converted into integer values before the shift operation.
For example:
Class Test
{
Public static void main (string ARGs [])
{
Byte x = 127;
Byte y = (byte) (x> 9 );
System. Out. println (y );
}
}
Display: 0
For example:
Char c = 'l ';
System. Out. println (C> 1 );
Compiled to convert the character value to the int Value

5. If the boolean type is set to true, the bitwise value 1 corresponds to the bitwise value 0, and the return value is still 'boolean. &, |, ^, Can be used for boolean values, ~ It cannot be used as a Boolean value.
Boolean Type true, false can be compared, but only = can be used, <, <=... will generate a compilation error. In addition, the boolean type cannot be compared with other types of values.
For example:
Class Test
{
Public static void main (string ARGs [])
{
Boolean x = true;
Boolean y = false;
System. Out. println (X & Y) + "" + (X & X ));
System. Out. println (x ^ y) + "" + (y ^ y ));
System. Out. println (X | Y) + "" + (Y | y ));
}
}
Display: false True False false true false

The six-digit operation requires that the operand be an integer. the operand cannot be a string or a decimal number.
For example:
String S = "hello ";
Long L = 99;
Double D = 1.11;
Int I = 1;
Int J = 0;
A: J = I <s;
B: J = I <J;
C: J = I <D;
D: J = I <L;
Correct: B, d
For example:
Char c = 'l ';
System. Out. println (C> 1 );
Compiled to convert the character value to the int Value
Integer I = INTEGER ("1 ");
System. Out. println (I> 1 );
Compilation failed
7 because bitwise operations are binary operations, they cannot be mixed with some Octal numbers. Binary in Java has no notation.
For example:
System. Out. println (010 | 4 );
Display: 12
In addition, if a negative number is encountered in the bitwise operation, it must be converted into a complement code before calculation. Instead of using the original code.
For example:
Class Test
{
Public static void main (string ARGs [])
{
Try
{
Int x =-7;
System. Out. println (X/2 + "" + (x> 1 ));
}
Catch (exception E)
{
System. Out. pritnln ("exception ");
}
}
}
A A compiler Error
B exception
C-3-3
D-3-4
E-4-4
F none of the above
Select: F
-7> 1:-7: 11111111 11111111 11111111
Subsequent operations: 11111111 11111111 11111111 11111100
For-7/2, it is still the same as positive division, which is-3.
For example:
Int A =-1;
Int B =-1;
A = A >>> 31;
B = B> 31;
Display: a = 1, B =-1
In part B, insert 1 at a high position, 31 at a time, and 32 at a time. This is a complement code, and then convert it to-1 in decimal format.

Slightly complex:
Int I = 1;
I <= 31;
I >>= 31;
I >>= 1;
Int J = 1;
J <= 31;
J> = 31;
System. Out. println ("I =" + I );
System. Out. println ("J =" + J );
Display: I =-1 J =-1
I <= 31: 1000 0000 0000 0000 0000 0000 0000 0000 // It is regarded as a negative number.
I >>= 31: 1111 1111 1111 1111 1111 1111 1111
I >>= 1: 1111 1111 1111 1111 1111 1111 1111

8 ~ Operation, you can take the negative minus one method based on the relationship between the binary positive and negative numbers in the computer, that is ~ I = (-I)-1.
For example:
Int I = 45678;
Int J = ~ I;
System. Out. println (j );
Result: (-45678)-1 =-45679
For example:
Class Test
{
Public static void main (string ARGs [])
{
Int x = 1;
Int y = ~ X + 1;
System. Out. println (x + "" + y );
}
}
Display: 1-1

9 operator priority and Operation Order: The operation order is from left to right, and the assignment order is from right to left.
Class Test
{
Public static void main (string ARGs [])
{
Int [] reftoarray = {10, 11 };
Int Var = 1;
Reftoarray [var-1] = Var = 2;
System. Out. println (reftoarray [0] + "" + reftoarray [1]);
}
}
The above output is 2 and 11. First, the array subscript is calculated, and then the value is assigned.
The operator = has a higher priority than the value assignment operator =.
For example:
Public class test
{
Public static void main (string ARGs [])
{
Int I = 10;
Int J = 10;
Boolean B = false;
If (B = I = J)
System. Out. println ("true ");
Else
System. Out. println ("false ");
}
}

Bitwise operators and comparison operators at the same level can be computed from left to right.
For example:
If (5 & 7> 0 & 5 | 2)
System. Out. println ("true ")
Show: Compilation Error
5 & 7 is the first calculation, and then the result of 5 & 7 is greater than 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.