- #1
noblerare
- 50
- 0
Homework Statement
I want to write the following if statement into simply manipulations using the bit operations: ! ^ & | << >> ~ +
Given an x...
if ( x == -1 || x == 0)
return -1;
else
return 0;
I am dealing with 32-bit integers. How do I go about doing this? (Note: -1 is represented by all 1s and 0 is represented by all 0s. 0b111...111 = -1 and 0b000...000 = 0
The Attempt at a Solution
My attempt:
(-1 & something) | 0
In other words, I am trying to return -1 on one branch and 0 on the other branch. The "something" needs to determine whether or not x is -1 or 0.
If I do (-1 | x), it will return -1 when x is either -1 or 0 which is good but it does NOT return 0 when x is anything else. In fact, it always returns -1...
Last edited: