Bitwise operator in java MCQ Questions and Answers
Table of Contents
Study and learn Java MCQ Questions and Answers on bitwise operator in java and their priorities. Attend job interviews easily with these Multiple Choice Questions
Java language allows developers to work with individual bits of a number using Logical Bitwise Operators. Bitwise logical operators and Logical Java Bitwise Operators are one and the same. Simply use the term “Bitwise Operators” to refer to bitwise operators. Do not use the word Logical.
bitwise operator in java vs Logical Operators in Java
bitwise operator in java | Logical Operators in java |
---|---|
Bitwise operators work with integer type data | Logical operators work with boolean type data.So use LOGICAL to refer to boolean logical operators. |
If boolean operands are present, use Bitwise Operators. | If Relational operators are present, use Logical Operators. |
Bitwise operations are used less in general. | Programmers use logical operators very often. |
Bitwise operators are ~, &, |, ^, <<, >>, >>> and op=. | Logical operators are !, &, &&, |, ||, ^ and op=. |
bitwise operator in java
The common operators used in both Bitwise Operation and Logical Operation are as follows.
- AND – &
- OR – |
- Exclusive OR or XOR – ^
- Compound AND – &=
- Compound OR – |=
- Compound XOR ^=
Bitwise Operators and Priority in java
Each integer type number contains so many bits of information. The purpose of bitwise operators is to manipulate individual bits of an integer type number.
Supported Five Integer Types for Bitwise Operation are as follows.
- byte (8 bits)
- char (16 bits) – unsigned
- short (16 bits)
- int (32 bits)
- long (64 bits)
We have shown 6 different groups of bitwise operators with priority. This priority or precedence is used to resolve a deadlock in solving equal priority operations in an arithmetic expression.
Priority | Operator | Simple Name |
---|---|---|
1 | TILDE ~ | Bitwise Unary NOT |
2 | >> | Shift Right |
2 | << | Shift Left |
2 | >>> | Shift Right Zero Fill |
3 | & Ampersand | Bitwise AND |
4 | ^ CARAT | Bitwise Exclusive OR |
5 | | PIPE | Bitwise OR |
6 | op= | Assignment Operatora op= ba = a op b |
6 | &= | Bitwise AND Assignmenta &=ba= a&b |
6 | |= | Bitwise OR Assignmenta |=ba= a|b |
6 | ^= | Bitwise Exclusive OR Assignmenta ^=ba= a^b |
6 | >>= | Bitwise Shift Right Assignmenta >>=ba= a>>b |
6 | >>>= | Bitwise Shift Right Fill Zero Assignmenta >>>=ba= a>>>b |
6 | <<= | Bitwise Shift Left Assignmenta <<=ba= a<<b |
Bitwise operators have less priority than arithmetic and relational operators. Only Bitwise Unary NOT or Bitwise Complementary Operator (~) has the highest priority than arithmetic and relational operators.
Bitwise Operator Priority in java example
class BitwisePriority { public static void main(String args[]) { int a=5; 0101 int b=6; 0110 //12 1100 - // 5 0101 - int c = a&b+6; System.out.println(c); } } //OUTPUT //a&(b+6) //a&12 //5&12 //4
Note: We have used Binary Literals in this example. A binary literal starts with 0b. You can specify a number in binary format using this notation. Also, “op” refers to “Operand” in this tutorial.
1. Bitwise Unary NOT Operator (~ TILDE)
Bitwise Unary NOT operator (~) is also called Bitwise Complement Operator. It simply turns bit 0 to bit 1 and bit 1 to bit 0. We have used Type Casting to byte data type in most of the examples here.
Usage example:
class UnaryNOT { public static void main(String args[]) { byte a = 0b00001010; //a=10 byte b = (byte)~a; System.out.println(a + ", " + b); } } //OUTPUT: 10, -11
Operand | Result |
---|---|
0 | 1 |
1 | 0 |
2. Bitwise AND Operator (&)
Bitwise AND operator gives an output of 1 only if both the input bits are 1s. Even if one input bit is 0, the output is 0. Here operand is synonymous to input bit.
Usage example:
class BitwiseAND { public static void main(String args[]) { byte a = 0b00001010; //a=10 byte b = 0b00111001; //b=57 //00001000 = 8 byte c = a & b; System.out.println(a + ", " + b + "= " + c); } } //OUTPUT: 10, 57 = 8
Op1 | Op2 | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
3. Bitwise OR Operator (|)
Bitwise OR Operator (|) gives an output of 1 at least if one input bit is 1. Only if both the input bits are 0s, the output is 0.
Usage example:
class BitwiseOR { public static void main(String args[]) { byte a = 0b00001010; //a=10 byte b = 0b00111001; //b=57 //00111011 = 59 byte c = a | b; System.out.println(a + ", " + b + "= " + c); } } //OUTPUT: 10, 57 = 59
Op1 | Op2 | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
4. Bitwise Exclusive OR Operator (^)
Bitwise XOR Operator (|) or Bitwise Exclusive OR operator gives an output of 1 if both the input bits are different i.e 1,0 or 0, 1. If both the input bits are same i.e 0,0 or 1,1, the output is 0.
Usage example:
class BitwiseXOR { public static void main(String args[]) { byte a = 0b00001010; //a=10 byte b = 0b00111001; //b=57 //00110011 = 51 byte c = a ^ b; System.out.println(a + ", " + b + "= " + c); } } //OUTPUT: 10, 57 = 51
Op1 | Op2 | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
5. Bitwise Shift Left or Left Shift Operator Operator (<<)
Bitwise Shift Left or Right Shift operator shifts the individual bits from Right to Left. So gaps are formed on the right side. These gaps are filled with 0s. Left most bits are discarded.
Note: If discarded bits on the left side are zeroes, Left Shift operation is equivalent to multiplying the number by 2.
Usage example:
class BitwiseLeftShift { public static void main(String args[]) { byte a = 0b00001010; //a=10 //00010100 //20 byte b = (byte)(a << 1); System.out.println(a + ", " + b); byte c = 0b01010111; //87 //0101-01110000 //Left 4 bits discarded byte d = (byte)(c << 4); System.out.println(c + ", " + d); } } //OUTPUT: //10, 20 //87, 112
6. Bitwise Shift Right or Right Shift Operator Operator (>>)
Right Shift Operator shifts the individual bits of a number towards Right Side from the Left Side. Usually Left Most bits are filled with Zeroes in the process of filling gaps created because shifting bits to the right side. Shift Right operator does Sign Extension by putting the Left Most Bits to 1s if they are already 1s. So, negative numbers are still negative numbers after the Right Shift operation.
Note: Right Shift operation is equivalent to dividing the number by 2.
Usage example:
class BitwiseRightShift { public static void main(String args[]) { byte a = 0b00010100; //a=20 //00001010 //10 byte b = (byte)(a >> 1); //=20/2 System.out.println(a + ", " + b); byte c = 0b01010111; //87 //00000101-0111 //5 //Right 4 bits discarded byte d = (byte)(c >> 4); //=87/(2*2*2*2) System.out.println(c + ", " + d); byte e = (byte)0b11111000; //-8 //11111100 //-4 //Right 1 bit discarded byte f = (byte)(e >> 1); System.out.println(e + ", " + f); } } //OUTPUT: //20, 10 //87, 5 //-8, -4
7. Bitwise Shift Right Zero Fill Operator (or) Unsigned Right Shift Operator (>>>)
Unsigned Shift Right or Shift Right Fill Zero operator shifts the individual bits of a number from Left to Right. It fills Zeroes on the left side in the gaps created by shifting the digits to the right. This operator does not maintain Sign Extension. So, a negative number may become a positive number after the Right Shift Fill Zero operation.
Note: Right Shift Fill Zero operation is equivalent to dividing the number by 2.
Usage example:
class BitwiseRightShiftUnsigned { public static void main(String args[]) { byte a = 0b00010100; //a=20 //00001010 //10 byte b = (byte)(a >>> 1); //=20/2 System.out.println(a + ", " + b); byte c = 0b01010111; //87 //00000101-0111 //5 //Right 4 bits discarded byte d = (byte)(c >>> 4); //=87/(2*2*2*2) System.out.println(c + ", " + d); int e = -1;//(byte)0b11111111; //11111111 11111111 11111111 11111111 //-1 //00000000 00000000 00000000 00001111 //15 int f = (int)(e >>> 28); System.out.println(e + ", " + f); } } //OUTPUT: //20, 10 //87, 5 //-1, -15
This is all about Bitwise Operators in Java. You should practice these examples on your PC for better understanding.
[WpProQuiz 85]
bitwise operator in java MCQ
1) Identify the Bitwise NOT operator in Java below.
A) !
B) &
C) ~
D) None of the above
Answer [=] C
Explanation:
It negates the bit 1 to 0 and bit 0 to 1.
2) Bitwise operators in Java work with?
A) boolean data like true or false
B) Real numbers like float or double
C) Individual bits of integers like byte, short, int, long and char
D) All the above
Answer [=] C
3) Find operators that work as both Logical operators and Bitwise operators in Java?
A) &, &=
B) |, |=
C) ^, ^=
D) All the above
Answer [=] D
4) If relational operators are present in an expression, what type of other operators may be used?
A) Logical operators
B) Bitwise operators
C) A and B
D) None of the above
Answer [=] A
Explanation:
Logical & is used here. (a>5) & (b<10)
5) What is the name of << bitwise operator in Java?
A) Right Shift Operator
B) Left Shift Operator
C) Left Shift Fill Zero operator
D) Right Shift Fill Zero operator
Answer [=] B
Explanation:
Left shift operator shifts individual bits from right side to the left side.
6) What is this >> bitwise operator in Java?
A) Left shift operator
B) Right shift operator
C) Left Shift Fill Zero operator
D) Right Shift Fill Zero operator
Answer [=] B
Explanation:
Right Shift operator shifts individual bits of a number from left side to the right side maintaining the sign of the number. So, a negative number is still a negative number after the right shift operation.
7) What is this >>> bitwise operator in Java?
A) Left Shift operator
B) Left Shift Fill Zero operator
C) Right Shift Operator
D) Right Shift Fill Zero operator
Answer [=] D
Explanation:
>>> (Right Shift Fill Zero) operator fills the left side gaps created by shifting with Zeros thus losing the sign of the number.
8) Left Shift (<<) in Java is equivalent to?
A) Subtracting the number by 2
B) Dividing the number by 2
C) Multiplying the number by 2
D) Adding the number by 2
Answer [=] C
Explanation:
byte a = 0b0000_0001; //1 a = a << 1; //a now holds 0000_0010 //2
9) Right Shift >> in Java is equivalent to?
A) Multiplying the number by 2
B) Dividing the number by 2`
C) Subtracting the number by 2
D) Adding the number by 2
Answer [=] B
Explanation:
byte a = 0b0000_0100; //4 a = a >> 1; //a now holds 0000_0010 i.e 2
10) What is the output of the Java code snippet?
byte a = 0b0000_0001; System.out.println(~a);
A) -1
B) -2
C) 254
D) +127
Answer [=] B
Explanation:
a = 0b0000_0001; //1 ~a = 1111_1110; //254 Byte rannge is -128 to +127. So, overflown 255 goes to the negative side
11) What does this Java code snippet prints?
int b=45; String str=""; while(b > 0) { str = str + b%2; b = b/2; } StringBuilder sb = new StringBuilder(str); sb.reverse(); System.out.println(sb.toString());
A) Prints the remainder of a number
B) Prints Binary representation of a number
C) Prints Octal representation of a number
D) Prints Hexadecimal representation of a number
Answer [=] B
Explanation:
The output is 101101 (45).
12) What is the output of the Java code snippet?
System.out.println(0b0000_1000);
A) 0b0000_1000
B) 1000
C) 8
D) 9
Answer [=] C
Explanation:
Binary literals start with 0b. Above number is 8 in decimal notation.
13) What is the output of a Bitwise AND (&) operation if both the inputs/operands are 1s?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] B
Explanation:
1 & 1 = 1
14) What is the output of a Bitwise OR (|) operation if both the inputs are 1s?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] B
Explanation:
1 | 1 = 1
15) What is the output of a Bitwise AND (&) operation if one of the inputs/operands is 0?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] A
Explanation:
0 & (anything) = 0
16) What is the output of a Bitwise OR (|) operation if one of the inputs/operands is 1?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] B
Explanation:
1 | (anything) = 1
17) What is the output of a Bitwise AND (&) operation if one of the inputs/operands is 1?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] C
Explanation:
1 & (P) = 1 if P=1 1 & (P) = 0 if P=0
18) What is the output of a Bitwise OR (|) operation if one of the inputs/operands is 0?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] C
Explanation:
0 | P = 1 if P==1 0 | P = 0 if P==0
19) What is the output of a Bitwise Exclusive OR (^) operation if both of the inputs/operands are 0s or 1s?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] A
Explanation:
0 ^ 0 = 0; 1 ^ 1 = 1;
20) What is the output of a Bitwise Exclusive OR (^) operation if both the inputs/operands are different?
A) 0
B) 1
C) 0 or 1
D) None of the above
Answer [=] B
Explanation:
0 ^ 1 = 1; 1 ^ 0 = 1;
bitwise operator in java