Rotate Right - How to deal with signed numbers?
int rotateRight(int x, int n) {
int temp = x;
x >>= n;
temp = temp << (32+((~n)+1));
// Now need to find a way to deal with two's comp numbers
// Since x has been shifted right, if it is neg than it has been
// Sign extended....have to AND the n shifted bits with 0 before
// or'ing with temp.
//int signFix = ....
return x | temp;
}
// Legal ops: ~ & ^ | + << >>
// Max ops: 25
The problem is sign extension causes an issue for signed numbers... The
amount of spaces shifted right in a signed number would be replaced by
1's. I need to and those numbers with 0 before or'ing with temp... How can
I accomplish this?? Any help would greatly be appreciated. I can only use
bitwise ops.
No comments:
Post a Comment