I am trying to set the least significant bit to all bit places. For instance, copyLSB(6)
would yield 0x00000000
whereas copyLSB(5)
would yield 0xffffffff
.
Here is my code:
int copyLSB(int x) {
int r = x << 31;
r = x >> 31;
return r;
}
The program works for 6 and 5. However, when I input 0x80000000
it fails and returns 0xffffffff
.
Hi guys, I found a way to do this without using bit shifting and this works on signed numbers too.
int copyLSB(int x) { │
int result = x & 1; │
return ~(~(x & 0) + result); │
}