<span class="greencolor2">Enrol for Java Certification</span>
Certifications Boost Confidence. Go through Certifications CENTER
Study and learn Java MCQ Questions and Answers on Bitwise Operators and their priorities. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Bitwise Operators before reading these objective questions.
byte b = 0b0000101; System.out.print(b + ","); b &= 0b00001111; System.out.print(b);
0101 & 1111 = 0101. &= is a Bitwise AND Shorthand Assignement operator.
byte b = 0b00000101; System.out.print(b + ","); b |= 0b00001111; System.out.print(b);
|= is a Bitwise OR Shorthand Assignment operator. a |= b ---> a = a|b;
byte b = 0b00000101; System.out.print(b + ","); b ^= 0b00001111; System.out.print(b);
Bitwise exclusive OR produces 1 for different Inputs and 0 for the same Inputs. 0101 ^1111 ------ 1010
byte b = 0b00000101; System.out.print(b + ","); b = (byte)~b; System.out.print(b);
Bitwise NOT(~) operator turns 1s to 0s and 0s to 1s. If the leftmost bit is 1, it is a negative number. So take 1's complement and add 1 to it. ~0000 0101 = 1111 1010 ----------- ~ 111 1010 = 000 0101 ----------- 000 0101 000 0001 + ----------- - 000 0110
byte b = 0b00000101; System.out.print(b + ","); b = (byte)(b << 1); System.out.print(b);
Left Shift 1 bit. Before: 0000 0101 After: left 0 removed <-- 0 [000 0 101 0] <--- 0 added at right =0000 1010
byte b = 0b00000101; System.out.print(b + ","); b = (byte)(b >> 1); System.out.print(b);
Right Shift Operator shifts bits rightside. Fills 0s on the left side. Before: 0000 0101 After [0 0000 010]1 = 0000 0010
byte num = (byte)0b1111_1000; System.out.print(num + ","); num = (byte)(num >> 1); System.out.print(num);
1111_1000 == ~111 1000 + 1 = 000 0111 + 1 = 000 1000 = -8 -8 >> 1 = 1111_1100 = ~ 111 1100 + 1 = 000 0011 + 1 = 000 0100 = -4
byte num = (byte)0b1111_1000; System.out.print(num + ","); num = (byte)(num >>> 1); System.out.print(num);
In this case, >> and >>> produce the same output. A negative number is a negative number only.
byte num = (byte)0b000_1000; System.out.print(num + ","); num = (byte)(num >>> 1); System.out.print(num);
byte num = (byte)0b000_1000; if(num >> 1 > 6) { System.out.print(num); } else { System.out.println(num>>1); }
(num>>1)>6 4 > 6 = false
byte num = (byte)0b000_1000; if(num >> 1 > 6 || true) { System.out.print(num); } else { System.out.println(num>>1); }
Logical OR (||) is executed at last. (4>6) || true = false||true = true
byte num = (byte)0b000_1000; if(num >> 1 > 6 || true | 2>3) { System.out.print(num+1); } else { System.out.println(num>>2); }
Each logic block is evaluated separately as it has less priority.
Open Certification Helper Popup Reset Popup