2.64 Any Odd One

Problem:

Write code to implement the following function:

/* Return 1 when any odd bit of x equals 1; 0 otherwise */
int any_odd_one(unsigned x);

You may assume that data type int has w=32 bits.

code:

#include <stdio.h>
#include <assert.h>

/* Return 1 when any odd bit of x equals 1; otherwise. Assume w=32 */
int any_odd_one(unsigned x) {
    return !!(x & 0xaaaaaaaa);
}

int main() {
    unsigned a = 0x3;
    assert(any_odd_one(a));
    unsigned b = 0x4;
    assert(!any_odd_one(b));
    return 0;
}

Last updated