How is if-statement and bitwise operations same in this example?

Viewed 1641

I was reading this answer and it is mentioned that this code;

if (data[c] >= 128)
    sum += data[c];

can be replaced with this one;

int t = (data[c] - 128) >> 31;
sum += ~t & data[c];

I am having hard time grasping this. Can someone explain how bitwise operators achieve what if statement does?

3 Answers
Related