Returning 1 if both bits are are not zero

91 Views Asked by At

I'm trying to create a function where 0x04060002 and 0x00080008

would return 0x000X000X because each of the other positions have at least one zero. Any nonzero number can go where the "X" is.

I've tried &-ing and |-ing but I can't seem to find the right steps.

1

There are 1 best solutions below

0
On

I would solve this problem something like this (C code):

unsigned int nibble_or(unsigned int x, unsigned int y)
{
    return
        ((0xf0000000 & x) && (0xf0000000 & y)) << 28 |
        ((0x0f000000 & x) && (0x0f000000 & y)) << 24 |
        ((0x00f00000 & x) && (0x00f00000 & y)) << 20 |
        ((0x000f0000 & x) && (0x000f0000 & y)) << 16 |
        ((0x0000f000 & x) && (0x0000f000 & y)) << 12 |
        ((0x00000f00 & x) && (0x00000f00 & y)) <<  8 |
        ((0x000000f0 & x) && (0x000000f0 & y)) <<  4 |
        ((0x0000000f & x) && (0x0000000f & y)) <<  0 ;
}

Test:

int main()
{
    printf("nibble_or(0x04060002, 0x00080008) = 0x%08x\n", nibble_or(0x04060002, 0x00080008));
}

Prints: nibble_or(0x04060002, 0x00080008) = 0x00010001