I am writing a function is_zero
that is supposed to return 0
if x != 0
, or nonzero otherwise. I am not allowed to use any constants. For example, x == 0
is not allowed. (the == operator is not allowed either)
The only operators I am allowed to use are =
, ~
, ^
, *
(dereferencing), &
, |
, <<
, >>
and +
.
The way I have the function written now is it will return 0
if x != 0
, but it still returns 0
even when x == 0
, which is it not supposed to do. I have attempted all sorts of combinations, but this homework question appears impossible given the constraints. I am posting here as a last ditch effort.
Can anybody how me how I can get my code to return something other than 0
when x == 0
, while still returning 0
when x != 0
?
int is_zero(int x) {
return (x ^ x);
}
See Question&Answers more detail:os