I'm working with bitwise and logical operators in C. I'm familiar with concept and purpose of these operators, but have run into bit of a problem.
I need to determine whether an integer x
can fit into a short. I am restricted to using the operators ! ~ & ^ | + << >>
. Also, I can only use up to 8 of these operators.
I know from several other posts (like this one: How to tell if a 32 bit int can fit in a 16 bit short) that a solution like
!(((((x) & 0xffff8000) >> 15) + 1) & 0x1fffe)
would work just fine (credit to Emil Romanus). However, I'm also forbidden to use any constants that are not 0x0
and 0xff
.
I'm mostly confused on the logic here. Any ideas?
EDIT: I forgot to mention conditional statement are also forbidden. So no ifs, elses, loops etc.