Bitwise AND for division: Can the bitwise & operator be used to divide an integer by powers of 2 in C?\nChoose the correct statement.

Difficulty: Easy

Correct Answer: False

Explanation:


Introduction / Context:
This checks understanding of which bitwise operations correspond to division or modulo by powers of 2. Knowing the correct operator helps avoid bugs and write efficient low-level code.



Given Data / Assumptions:

  • We are operating on integers in C.
  • Bitwise operators include & (AND) and | (OR); shift operators include << and >>.


Concept / Approach:
Division by 2^n is performed by right shifting (x >> n) for unsigned integers and implementation-defined for signed with arithmetic right shift. Bitwise AND with a mask (x & (2^n - 1)) computes x modulo 2^n, not division.



Step-by-Step Solution:
To divide x by 4 (2^2), use x >> 2 (for unsigned x).Using x & 3 yields x mod 4, i.e., the remainder, not the quotient.Therefore, saying bitwise & can be used to divide is false.



Verification / Alternative check:
Test with x = 13: 13 >> 2 = 3 (division), while 13 & 3 = 1 (remainder). Clearly different.



Why Other Options Are Wrong:
True: confuses modulo with division.



Common Pitfalls:
Using the wrong operation for quotient vs. remainder; assuming bitwise tricks all do the same task.



Final Answer:
False

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion