A Collection of Bit Programming Interview Questions solved in C++ by Antonio Gulli

A Collection of Bit Programming Interview Questions solved in C++ by Antonio Gulli

Author:Antonio Gulli [Gulli, Antonio]
Language: eng
Format: epub
Published: 2014-05-22T00:00:00+00:00


Code

unsigned short parity(unsigned long n)

{

unsigned short result = 0;

while (n)

{

result ^= 1;

n &= (n - 1);

}

return result;

}

static int preComputedParity[1 << 16];

unsigned short partityFast(_int64 n)

{

int mask = 0xffff;

return preComputedParity[n >> 48] ^

preComputedParity[(n >> 32) & mask] ^

preComputedParity[(n >> 16) & mask] ^

preComputedParity[n & mask];

}

17.

Swap two integers variables with no additional memory

Solution

The solution uses XOR operation and it needs no additional storage. The interested reader can think about how to swap two variables which are not integers.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.