Discussion
Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?
Answer: A
Correct answer: Bitwise AND (&)
Parity lives entirely in the lowest bit: a binary number is odd exactly when bit 0 is 1, because every higher place value is a multiple of two.
So n & 1 isolates that bit and answers the question in one operation - non-zero for odd, zero for even. It is faster than n % 2 and, unlike the modulus, gives the right answer for negative numbers in two's complement, where % returns \( -1 \) for an odd negative value. OR and XOR combine bits rather than isolating one, and ~ inverts the whole value.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development