Another bitwise problem I'm working on now is this:
Anyway, here's my solution to this problem:Code:/* * bitMask - Generate a mask consisting of all 1's * lowbit and highbit * Examples: bitMask(5,3) = 0x38 * Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31 * If lowbit > highbit, then mask should be all 0's * Legal ops: ! ~ & ^ | + << >> * Max ops: 16 * Rating: 3 */ int bitMask(int highbit, int lowbit) { return 2; }
My code works fine except for one case: when the highbit is 31.Code:/* * bitMask - Generate a mask consisting of all 1's * lowbit and highbit * Examples: bitMask(5,3) = 0x38 * Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31 * If lowbit > highbit, then mask should be all 0's * Legal ops: ! ~ & ^ | + << >> * Max ops: 16 * Rating: 3 */ int bitMask(int highbit, int lowbit) { int i = 0, j = 0; i = ~i; i <<= highbit + 1; j = ~j; j <<= lowbit; i ^= j; return i & j; }
Somehow the bitwise operator works when it's:
But it won't shift the leftmost significant bit when I do this:Code:i <<= 31; // i is 11111111111111111111111111111111 (32 digits)
Can anyone tell me why my compiler won't shift the entire thing to the left? (I'm using VC++ 2003, btw)Code:i <<= 32; // nothing happens. in the next line, i is still -1
And how do I go about it?



LinkBack URL
About LinkBacks


