0

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.

tsent123
  • 11
  • 1
  • Those two values don't have any non-zero bits in common. Do you want to check if two nibbles (four bits) in same position are both non-zero? – md2perpe Sep 12 '18 at 19:41
  • I was under impression that bit can only be $1$ or $0$ – Vasili Sep 12 '18 at 19:41
  • Yes, I want to check groups of four bits. So 0x6 and 0x8 are both not 0x0 so it would return a nonzero value there. And yes, bits can only be 1 or 0, but 0x4 = 0100, etc.. – tsent123 Sep 12 '18 at 19:43
  • I don't think that you can do what you want in a single step. – md2perpe Sep 12 '18 at 19:46

1 Answers1

0

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

md2perpe
  • 26,770