0

How can I efficiently shift every bit in an integral type to the left, and wrap around so that the leftMost bit becomes the rightmost?

eg if the type was a byte, then:

1111,1110 becomes 1111,1101
0000,0001 becomes 0000,0010
1000,0000 becomes 0000,0001

At the moment I'm using this:

//num is an ulong
bool leftMostSet = (num & 0x8000000000000000) == 0x8000000000000000;
num = num << 1;
num = num | (leftMostSet ? (ulong)1 : 0);
return num;

I'm wondering if there is a more efficient method though

Yair Halberstadt
  • 5,733
  • 28
  • 60

0 Answers0