What does it mean to set a specific bit in a number?
Setting a specific bit means changing that bit to 1, regardless of its previous value. It is like turning on a light switch at a certain position.
Click to reveal answer
beginner
How do you clear a specific bit in a number?
Clearing a specific bit means changing that bit to 0, no matter what it was before. It is like turning off a light switch at a certain position.
Click to reveal answer
beginner
What does toggle a specific bit mean?
Toggling a bit means changing it from 0 to 1 or from 1 to 0. It flips the bit's value, like flipping a switch.
Click to reveal answer
intermediate
In Go, how do you set the 3rd bit (0-based) of an integer variable n?
Use the bitwise OR operator with 1 shifted left by 3: n = n | (1 << 3). This sets the 3rd bit to 1.
Click to reveal answer
intermediate
How do you clear the 2nd bit of an integer n in Go?
Use bitwise AND with the complement of 1 shifted left by 2: n = n & ^(1 << 2). This clears the 2nd bit to 0.
Click to reveal answer
Which operator is used to set a specific bit in Go?
ABitwise XOR (^)
BBitwise AND (&)
CBitwise OR (|)
DBitwise NOT (^)
✗ Incorrect
Setting a bit uses bitwise OR to turn that bit on.
How do you clear a bit at position k in Go?
An = n | (1 << k)
Bn = n & ^(1 << k)
Cn = n ^ (1 << k)
Dn = n & (1 << k)
✗ Incorrect
Clearing a bit uses AND with the complement of the bit mask.
What does toggling a bit do?
AFlips the bit from 0 to 1 or 1 to 0
BClears the bit to 0
CSets the bit to 1
DLeaves the bit unchanged
✗ Incorrect
Toggling flips the bit's value.
Which Go expression toggles the bit at position k of n?
An = n ^ (1 << k)
Bn = n | (1 << k)
Cn = n & ^(1 << k)
Dn = n << k
✗ Incorrect
XOR (^) with the bit mask toggles the bit.
If n = 8 (binary 1000), what is n after setting bit 1?
A0 (binary 0000)
B9 (binary 1001)
C12 (binary 1100)
D10 (binary 1010)
✗ Incorrect
Setting bit 1 (second bit) changes 1000 to 1010 which is 10.
Explain how to set, clear, and toggle a specific bit in an integer using bitwise operators in Go.
Think about turning bits on, off, or flipping them using masks.
You got /3 concepts.
Describe a real-life analogy for setting, clearing, and toggling bits.
Imagine bits as light switches in a room.
You got /3 concepts.
Practice
(1/5)
1. What does the operation num |= (1 << 3) do to the number num?
easy
A. Sets the 4th bit of num to 1
B. Clears the 4th bit of num to 0
C. Toggles the 4th bit of num
D. Checks if the 4th bit of num is set
Solution
Step 1: Understand bit shifting
The expression 1 << 3 shifts 1 left by 3 bits, resulting in binary 00001000, which targets the 4th bit.
Step 2: Apply bitwise OR to set bit
Using |= with this mask sets the 4th bit of num to 1 without changing other bits.
Final Answer:
Sets the 4th bit of num to 1 -> Option A
Quick Check:
Set bit = |= (1 << position) [OK]
Hint: Use OR with shifted 1 to set a bit [OK]
Common Mistakes:
Confusing set with clear operation
Using wrong bit position (off by one)
Thinking it toggles instead of sets
2. Which of the following Go code snippets correctly clears the 2nd bit (bit index 1) of an integer variable num?
easy
A. num &= (1 << 1)
B. num |= (1 << 1)
C. num &= ^(1 << 1)
D. num ^= (1 << 1)
Solution
Step 1: Identify clearing operation
To clear a bit, we AND num with the inverse mask that has 0 at the target bit and 1 elsewhere.
Step 2: Use bitwise AND with negated mask
^(1 << 1) creates a mask with all bits 1 except the 2nd bit 0. ANDing clears that bit.
Final Answer:
num &= ^(1 << 1) -> Option C
Quick Check:
Clear bit = &= ^(1 << position) [OK]
Hint: Use AND with negated shifted 1 to clear bit [OK]
Common Mistakes:
Using OR instead of AND to clear
Not negating the mask
Confusing bit index counting
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
num := 10 // binary 1010
num ^= (1 << 1)
fmt.Println(num)
}
medium
A. 10
B. 8
C. 12
D. 14
Solution
Step 1: Understand initial value and bit positions
Number 10 in binary is 1010. Bit positions (0-based) from right: bit0=0, bit1=1, bit2=0, bit3=1.
Step 2: Toggle 2nd bit (bit index 1) using XOR
XOR with (1 << 1) toggles bit1. Bit1 is 1, toggling it changes to 0. New binary: 1000 which is 8.
Final Answer:
8 -> Option B
Quick Check:
Toggle bit = ^= (1 << position) [OK]
Hint: XOR toggles bit: 1->0 or 0->1 [OK]
Common Mistakes:
Confusing toggle with set or clear
Misreading bit positions
Calculating decimal incorrectly
4. The following Go code is intended to clear the 3rd bit (bit index 2) of num. What is the error?
num := 15
num &= (1 << 2)
medium
A. It sets the 3rd bit instead of clearing it
B. No error, it correctly clears the 3rd bit
C. It toggles the 3rd bit instead of clearing
D. It clears all bits except the 3rd bit
Solution
Step 1: Analyze the operation performed
The code does num &= (1 << 2), which ANDs num with 00000100 (only 3rd bit set).
Step 2: Understand effect on bits
This clears all bits except the 3rd bit, effectively keeping only the 3rd bit if it was set, not clearing it.
Final Answer:
It clears all bits except the 3rd bit -> Option D
Quick Check:
Clear bit = &= ^(1 << position) [OK]
Hint: Use AND with negated mask to clear bit [OK]
Common Mistakes:
Using AND with mask instead of negated mask
Confusing clear with set or toggle
Assuming &= (1 << n) clears bit
5. You have an integer num initially 0. You want to toggle the 1st bit, set the 3rd bit, and clear the 2nd bit in this order. Which sequence of operations achieves this correctly in Go?
hard
A. num ^= (1 << 0); num |= (1 << 2); num &= ^(1 << 1)
B. num |= (1 << 0); num ^= (1 << 2); num &= ^(1 << 1)
C. num &= ^(1 << 1); num ^= (1 << 0); num |= (1 << 2)
D. num |= (1 << 2); num ^= (1 << 0); num &= ^(1 << 1)
Solution
Step 1: Toggle 1st bit (bit 0)
Use XOR with (1 << 0) to toggle the 1st bit: num ^= (1 << 0).
Step 2: Set 3rd bit (bit 2)
Use OR with (1 << 2) to set the 3rd bit: num |= (1 << 2).
Step 3: Clear 2nd bit (bit 1)
Use AND with negated mask to clear 2nd bit: num &= ^(1 << 1).
Final Answer:
num ^= (1 << 0); num |= (1 << 2); num &= ^(1 << 1) -> Option A
Quick Check:
Toggle, Set, Clear in order with ^=, |=, &= ^ [OK]
Hint: Toggle ^=, Set |=, Clear &= ^ in correct order [OK]
Common Mistakes:
Mixing order of operations
Using wrong operators for each action
Not negating mask when clearing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.