Give each bit a precise meaning before using masks, shifts or XOR. Distinguish logical from bitwise operations, setting from toggling, fixed-width complements, and parity cancellation in an integer fold.
Recognize it when A compact state consists of independent yes/no flags.
Checking your account…
Bit Manipulation Code Labs
Treat Integers as Bit Sets
A bit position represents one Boolean flag inside an integer. Build masks with one shifted by the zero-based index and name which position each mask owns.
Test Set and Clear One Bit
Test with AND, set with OR, clear with AND against the inverted mask, and toggle with XOR. Parenthesize shifts and combinations so precedence is visible.
Implement bit_operations(value,index). Return [tested,set_value,cleared_value,toggled_value] for the selected zero-based bit. The zero-based index must be a nonnegative integer.
Public tests
Apply one explicit mask
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
XOR is associative, commutative, and self-canceling: x XOR x is zero. It can isolate one unpaired value when every other value appears exactly twice, but the frequency premise is essential.
Find the Unpaired Value
Reference
def single_unpaired(values):
result=0
for value in values:result^=value
return result
Practice
Implement single_unpaired(values). Every integer appears twice except one; return the singleton using XOR.
Public tests
Cancel equal pairs with XOR
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Find the Unpaired Value
def single_unpaired(values):
result=0
for value in values:result^=value
return result
Watch Nonadjacent Pairs Cancel
Use the existing XOR Lab on [6, 3, 6, 5, 3]. Write the accumulator after each value before running it. Every other value occurs exactly twice, as required by this Lab. A second singleton call confirms that the initial zero does not erase a value.
Step
Executed statement
State
Why
Identity
result=0
result=0
Zero contributes no bits to XOR.
First two values
result^=value
after 6: 6; after 3: 5
The accumulator is the XOR of the processed prefix, not its sum.
Second 6
result^=value
result=3
The two 6 values cancel even though another value separated them.
Consume 5 and 3
result^=value
after 5: 6; after 3: 5
The pair of 3 values now cancels, leaving only 5.
Return
return result
5
The parity premise makes this remaining XOR the requested unpaired value.
Print
Output
print(single_unpaired([6, 3, 6, 5, 3]))
5
print(single_unpaired([9]))
9
Cancellation is a contract-dependent shortcut. If two different values each occur an odd number of times, their XOR alone does not identify either value. Choose a frequency map when occurrence counts are the real requirement.
Handle Python Signed Integers Deliberately
Python integers have arbitrary precision and negative values behave like infinite two’s-complement for bit operations. Apply an explicit width mask when the problem assumes 32 or 64 bits; avoid loops that expect a negative number to shift to zero.
Explain It in an Interview
Say: “This mask owns bit i. AND tests it, OR sets it, AND-not clears it, and XOR toggles it.” For cancellation, state the exact multiplicity contract and derive O(n) time and O(1) space.
Template To Memorize
Learn this shape by heart; most problems in this family change only one or two lines of it.
Bit manipulation basics
x & (x - 1) drops the lowest one; XOR cancels pairs.
The problem talks about bits, or asks for an O(1)-space trick with pairs of numbers.
Trigger words
single number
xor
count bits
reverse bits
power of two
without + or -
def count_ones(n):
count = 0
while n:
n &= n - 1 # clear the lowest set bit
count += 1
return count
def single_number(nums):
result = 0
for num in nums:
result ^= num # pairs cancel: a ^ a == 0
return result
Spot it when
'Single number', 'count bits', 'reverse bits'.
Add numbers without + or -.
Remember
n & 1 reads the lowest bit; n >> 1 drops it.
XOR is its own inverse: a ^ b ^ b == a.
How problems bend it
Counting bits for 0..n: bits[i] = bits[i >> 1] + (i & 1).
Reverse 32 bits: Repeat 32 times: result = (result << 1) | (n & 1); n >>= 1.
Python Version Note
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Bit Manipulation proof does not depend on a minor Python release.