What does the AND (&) bitwise operator do between two bits?
The AND operator returns 1 only if both bits are 1; otherwise, it returns 0. Example: 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0.
Click to reveal answer
beginner
Explain the OR (|) bitwise operator with a simple example.
The OR operator returns 1 if at least one of the bits is 1; otherwise, it returns 0. Example: 1 | 0 = 1, 0 | 0 = 0, 1 | 1 = 1.
Click to reveal answer
beginner
What is the result of XOR (^) between two bits?
XOR returns 1 if the bits are different, and 0 if they are the same. Example: 1 ^ 0 = 1, 1 ^ 1 = 0, 0 ^ 0 = 0.
Click to reveal answer
beginner
Describe the NOT (~) bitwise operator and its effect on a bit.
The NOT operator flips the bit: 0 becomes 1, and 1 becomes 0. Example: ~0 = 1, ~1 = 0 (in single bit context).
Click to reveal answer
beginner
What happens when you perform a left shift (<<) on a binary number?
Left shift moves all bits to the left by the specified number of positions. Each shift to the left multiplies the number by 2. Example: 0001 (1) << 2 = 0100 (4).
Click to reveal answer
What is the result of 5 & 3 in binary? (5 = 0101, 3 = 0011)
A0100 (4)
B0111 (7)
C0001 (1)
D0010 (2)
✗ Incorrect
AND compares bits: 0101 & 0011 = 0001, which is 1 in decimal.
Which operator flips all bits of a number?
ANOT (~)
BAND (&)
COR (|)
DXOR (^)
✗ Incorrect
NOT (~) flips every bit from 0 to 1 and 1 to 0.
What does the expression 6 ^ 3 evaluate to? (6 = 0110, 3 = 0011)
A0001 (1)
B0101 (5)
C0111 (7)
D0010 (2)
✗ Incorrect
XOR compares bits: 0110 ^ 0011 = 0101, which is 5.
What is the decimal result of 4 << 1?
A6
B2
C5
D8
✗ Incorrect
Left shift by 1 multiplies by 2: 4 << 1 = 8.
Which bitwise operator returns 1 if either bit is 1?
AOR (|)
BAND (&)
CXOR (^)
DNOT (~)
✗ Incorrect
OR (|) returns 1 if at least one bit is 1.
Explain how the AND, OR, and XOR bitwise operators work with examples.
Think about comparing two bits and when the result is 1.
You got /4 concepts.
Describe what happens when you use left shift (<<) and right shift (>>) operators on a binary number.
Imagine moving bits like sliding blocks left or right.
You got /4 concepts.
Practice
(1/5)
1. What does the bitwise AND operator (&) do when applied to two bits?
easy
A. Flips the bits from 0 to 1 and 1 to 0
B. Returns 1 if at least one bit is 1
C. Returns 1 only if both bits are 1
D. Returns 0 only if both bits are 0
Solution
Step 1: Understand bitwise AND operation
The AND operator compares each bit of two numbers and returns 1 only if both bits are 1.
Step 2: Apply to example bits
For bits 1 & 1, result is 1; for 1 & 0 or 0 & 1 or 0 & 0, result is 0.
Final Answer:
Returns 1 only if both bits are 1 -> Option C
Quick Check:
AND = 1 only if both bits 1 [OK]
Hint: AND returns 1 only when both bits are 1 [OK]
Common Mistakes:
Confusing AND with OR operator
Thinking AND flips bits
Assuming AND returns 1 if any bit is 1
2. Which of the following is the correct syntax for left shifting an integer variable x by 3 positions in Java?
easy
A. x << 3;
B. x >> 3;
C. x <<< 3;
D. x >>> 3;
Solution
Step 1: Recall left shift operator syntax in Java
Left shift operator is << and shifts bits to the left.
Step 2: Identify correct usage
To shift variable x left by 3 bits, use x << 3;. Other options are right shifts or invalid.
Final Answer:
x << 3; -> Option A
Quick Check:
Left shift = x << number [OK]
Hint: Use << for left shift, >> for right shift [OK]
Common Mistakes:
Using >>> for left shift (it's unsigned right shift)
Using <<< which is invalid syntax
Confusing left and right shift operators
3. What is the output of the following Java code?
int a = 5; // binary 0101
int b = 3; // binary 0011
int c = a ^ b;
System.out.println(c);
medium
A. 8
B. 7
C. 2
D. 6
Solution
Step 1: Convert numbers to binary
a = 5 is 0101, b = 3 is 0011 in binary.
Step 2: Apply XOR operation bitwise
XOR returns 1 if bits differ, 0 if same. 0101 XOR 0011 = 0110 (binary) which is 6 decimal.
Final Answer:
6 -> Option D
Quick Check:
5 ^ 3 = 6 [OK]
Hint: XOR sets bits where inputs differ [OK]
Common Mistakes:
Confusing XOR with OR or AND
Calculating XOR incorrectly bitwise
Assuming XOR adds numbers
4. Identify the error in the following Java code snippet:
int x = 8;
int y = ~x << 2;
System.out.println(y);
medium
A. No error; the code runs and prints a valid integer
B. The shift operator << must be used before the NOT operator ~
C. The bitwise NOT operator (~) cannot be used on integers
D. The variable y should be declared as long instead of int
Solution
Step 1: Analyze bitwise NOT and shift usage
The NOT operator (~) flips all bits of x. Then the result is shifted left by 2 bits.
Step 2: Check Java syntax and types
Both ~ and << operators work on int type. The expression is valid and prints an integer.
Final Answer:
No error; the code runs and prints a valid integer -> Option A
Quick Check:
~ and << valid on int [OK]
Hint: Bitwise NOT and shifts work fine on int in Java [OK]
Common Mistakes:
Thinking ~ cannot be used on int
Assuming operator order is wrong
Believing int can't hold shifted values
5. Given an integer n, which expression correctly toggles (flips) the 4th bit (counting from 0) of n using bitwise operators in Java?
hard
A. n = n | (1 << 4);
B. n = n ^ (1 << 4);
C. n = n & ~(1 << 4);
D. n = ~n << 4;
Solution
Step 1: Understand toggling a bit
Toggling means flipping the bit: 0 becomes 1, 1 becomes 0.
Step 2: Identify correct operator for toggling
XOR (^) with 1 at the bit position flips that bit. So n ^ (1 << 4) toggles the 4th bit.
Final Answer:
n = n ^ (1 << 4); -> Option B
Quick Check:
Toggling bit uses XOR with mask [OK]
Hint: Use XOR with mask to toggle bits [OK]
Common Mistakes:
Using OR to toggle (only sets bit)
Using AND with negation to toggle (clears bit)
Using NOT and shift incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.