I need to compute a number (a/(2**b) using only bitwise operators such as ! & ^ ~ and shifts. I was given the following hint but I’m new to C and I dont know what the code means:
int bias = x>0 ? 0 : ((1<<n)-1);
Can anyone explain it to me?
I thought a>>b would work but I dont think it works for negative numbers.
That particular bit of code gives you a bias of 0 if x is positive. Otherwise it produces a mask of the lower n bits. The
x = a ? b : c;pattern is called the ternary operator(technically the ‘conditional operator’, apparently) in C.