c - Do this using bit operators -


i want eval if integer number between 0 , 255 equal 0 or equal 224.

i have code:

if (!num || 224 == num) 

is there way using bitwise operation?

i tried , have valid: 0, 31, 32, 63 ,64, 95, 96, 127, 128, 159, 160, 191, 192, 223, 224, 255. obvious bad because need 0 , 224.

!((num+1) & 30) 

yes, , range of input makes easier.

you can use (x - 1) >> 31 detect whether x 0 (technically not portable, work)

it relies on x never being smaller 0, and, non-portably, on right shift on signed int being arithmetic shift (which case).

similarly, can use ((x ^ 224) - 1) >> 31 detect 224.

just put them this:

int mask = ((x - 1) >> 31) | (((x ^ 224) - 1) >> 31); 

and there go.