Since the integer is known to be non-negative, one can simply OR together all of the bits of the integer one-by-one. The result is the desired predicate. A simple loop that performs bit-wise OR-ing until the source operand is exhausted requires a comparison, which is not allowed.
The only workable alternative under the restrictions imposed that I can see right now is to use straight-line code that repeats the bit extraction process for as many steps as the integer has bits. This is what I use in the ISO-C99 code below. Each bit is extracted with DIV and MOD. OR of two one-bit variables s
and t
is computed as s + t - s * t
. A simple exhaustive test for 32-bit integers confirms that this approach if functional, but the efficiency is obviously not great.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define OR1(s,t) (s + t - s * t)
// (x >= 1) ? 1 : 0
uint32_t ge1 (uint32_t x)
{
uint32_t y;
y = OR1 (
OR1 (
OR1 (OR1 (OR1 ((x / 0x00000001) % 2, (x / 0x00000002) % 2),
OR1 ((x / 0x00000004) % 2, (x / 0x00000008) % 2)),
OR1 (OR1 ((x / 0x00000010) % 2, (x / 0x00000020) % 2),
OR1 ((x / 0x00000040) % 2, (x / 0x00000080) % 2))),
OR1 (OR1 (OR1 ((x / 0x00000100) % 2, (x / 0x00000200) % 2),
OR1 ((x / 0x00000400) % 2, (x / 0x00000800) % 2)),
OR1 (OR1 ((x / 0x00001000) % 2, (x / 0x00002000) % 2),
OR1 ((x / 0x00004000) % 2, (x / 0x00008000) % 2)))),
OR1 (
OR1 (OR1 (OR1 ((x / 0x00010000) % 2, (x / 0x00020000) % 2),
OR1 ((x / 0x00040000) % 2, (x / 0x00080000) % 2)),
OR1 (OR1 ((x / 0x00100000) % 2, (x / 0x00200000) % 2),
OR1 ((x / 0x00400000) % 2, (x / 0x00800000) % 2))),
OR1 (OR1 (OR1 ((x / 0x01000000) % 2, (x / 0x02000000) % 2),
OR1 ((x / 0x04000000) % 2, (x / 0x08000000) % 2)),
OR1 (OR1 ((x / 0x10000000) % 2, (x / 0x20000000) % 2),
OR1 ((x / 0x40000000) % 2, (x / 0x80000000) % 2)))));
return y;
}
int main (void)
{
uint32_t x, res, ref;
x = 0;
do {
res = ge1 (x);
ref = x >= 1;
if (res != ref) {
printf ("error: x=%08x res=%08x ref=%08x\n", x, res, ref);
return EXIT_FAILURE;
}
x++;
} while (x);
printf ("test passed\n");
return EXIT_SUCCESS;
}