Home > database >  Bitwise AND with 0 is unreachable in C
Bitwise AND with 0 is unreachable in C

Time:07-03

I want to check if the LSB is 0.

if(some_size_t & 1){} works fine

But why is if(some_size_t & 0){//This parts is unreachable} never reachable?

CodePudding user response:

Because in order to ever get the value 1 i.e. true both operands for the logical and bitwise & i.e. AND operator have to be 1. Its so called truth table is

op1 | op2 | op1 AND op2
=====================
0   | 0   | 0
1   | 0   | 0
0   | 1   | 0
1   | 1   | 1

Because your value, e.g. op2, 0 has only zeros, no ones, you will always get only zeros as a result, no matter the other operand. And 0 will evaluate to false. It's what we call a contradiction in logic, often noted with a up side down T or \bot in latex.

As then the if condition is always false, the code in its body will never be executed, i.e. is unreachable.

  • Related