Given an unsigned 32-bit integer x, which of the following C/C++ expressions correctly toggles m bits starting from position p (with the least significant bit at position 0)?
Assume: x is the input integer; p is the starting position of the bit range (0-based, LSB at position 0); m is the number of bits to toggle; and No overflow or invalid input conditions occur.
Step-by-step Solution:
\[ \textbf{Problem: } \quad \text{Given an unsigned 32-bit integer } x, \text{ toggle } m \text{ bits starting from position } p. \] --- \[ \textbf{Step 1: Generate a mask of } m \text{ ones.} \] \[ (1 \ll m) - 1 \quad \longrightarrow \quad \underbrace{11\ldots1}_{m \text{ ones}} \] --- \[ \textbf{Step 2: Shift the mask left by } p \text{ positions.} \] \[ ((1 \ll m) - 1) \ll p \] This creates a mask of \(m\) consecutive 1’s starting at bit position \(p\). --- \[ \textbf{Step 3: XOR with } x \text{ to toggle those bits.} \] \[ x \oplus \Big(((1 \ll m) - 1) \ll p\Big) \] --- \[ \boxed{x \; \wedge \; (((1 \ll m) - 1) \ll p)} \]