Example:
x = 0xF0AA
, now when I do x = x << 8
, I will get x == 0xAA00
.
Right side (8 bits) of new value is filled with zeros. Is there a method int .NET Framework which can fill this bits with that bits from left side (with disapeared part)?
Result should be x == 0xAAF0
.
Asked
Active
Viewed 132 times
2

apocalypse
- 5,764
- 9
- 47
- 95
-
Sounds like you want a bit/byte rotate operation. – leppie May 07 '13 at 06:41
-
1http://en.wikipedia.org/wiki/Circular_shift – JLRishe May 07 '13 at 06:43
-
I've not tested this as I've not got visual studio on this computer but just `(x<<8) + (x>>8)`? – Sayse May 07 '13 at 06:44
1 Answers
2
What you want to do is called a circular shift. It's easy enough to emulate with two shifts and an or.
UInt32 RotateLeft(Uint32 n, int howManyBits) {
return n << howManyBits | n >> (32 - howManyBits);
}

Hampus Nilsson
- 6,692
- 1
- 25
- 29
-
After I post my question, I was enlighten how to achieve it like you show it now. – apocalypse May 07 '13 at 07:55