(adsbygoogle = window.adsbygoogle || []).push({}); 1. The problem statement, all variables and given/known data

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

3. 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....

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: Bit operations conditional

**Physics Forums | Science Articles, Homework Help, Discussion**