Very basic question that is tripping me up: Write a function that produces the same thing as this:
int mult_3_div_4(int x){return (x*3)/4;}
But only using ! ~ & + << >> bitwise operators
Divide by 4 is of course << 2 So I tried something like:
int test(int x) {return ((x<<1 + x) >> 2);}
But I can't seem to find anything that matches x*3 using bitwise operators
The bitwise shifts <<
>>
have lower precedence that binary operators +
-
.
So the line should be...
int test(int x) {return ((x<<1) + x) >> 2;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With