Question: I'm trying to understand how bits and bit manipulation works. Could someone please explain what the following code does line by line. Say what the
I'm trying to understand how bits and bit manipulation works. Could someone please explain what the following code does line by line. Say what the function is trying to do and show a representation of how it works on a 32 bit register.
/* howManyBits - return the minimum number of bits required to represent x in * two's complement * Examples: howManyBits(12) = 5
*/
int howManyBits(int x) {
int a = x >> 31;
int b = ((~x) &a ) + (x & ( ~a));
int c = !!(b >> 16);
int d = 8 + (c << 4);
int f = !!(b >> d);
int g = d + (~3) + (f << 3);
int h = !!(b >> g);
int i = g + (~1) + (h << 2);
int j = !!(b >> i);
int k = i + (~0) + (j<<1);
int l = !!(b >> k);
int m = k + l + !!b;
return m;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
