R
roman ziak
77scrapper77 said:hi,
I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.
My program so far looks like:
int isNonZero(int x) {
x = (1 << x) & x;
x = ~x;
x = x & 1;
return x;
}
I am not sure if standard defines the case when you are shifting left by
more than the bit size of integer.
But how about:
N-1
---
[ \ (x >> i) ] & 1
/
---
i=0
where N is the bit size of word, so for 4 bit machine the expression is:
( x + (x>>1) + (x>>2) + (x>>4) ) & 1