Question 94

Computer Awareness Introduction to C/C++ Hard

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.

(A) \(x | ((1 << m) << p)\)
(B) \(x \& \sim(((1 << m) - 1) << p)\)
(C) \(x \wedge ((1 << m) << p)\)
(D) \(x \wedge (((1 << m) - 1) << p)\)
✓ Correct Answer: Option D Verified Solution

📘 Step-by-Step Solution & Detailed Explanation:

\[ \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)} \]

Practice Similar Introduction to C/C++ Questions

Reinforce your conceptual grasp with related questions from NIMCET 2025:

Q88 Consider a 9-bit representation. Which of the following correctly gives...
Solve →
Q89 What is the equivalent decimal value of the unsigned binary number...
Solve →
Q90 Dynamic RAM (DRAM) stores each bit of data in a separate capacitor. Due to...
Solve →
Q91 Consider the Boolean expression \(X = (A+B).C \) over Boolean variables A,...
Solve →
Q92 What is the hexadecimal representation of the decimal number 265?
Solve →
Full-Length Test Series

Prepare for NIMCET Under Real Exam Conditions

Take timed full-length mock tests with authentic question distributions, negative marking (+12/-3 in Math), and instant All India Rank calculation.

Start Free Mock Test Series →