Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to perform a circular bit shift in C#?

I know that the following is true

int i = 17; //binary 10001
int j = i << 1; //decimal 34, binary 100010

But, if you shift too far, the bits fall off the end. Where this happens is a matter of the size of integer you are working with.

Is there a way to perform a shift so that the bits rotate around to the other side? I'm looking for a single operation, not a for loop.

like image 809
Jason Z Avatar asked Aug 29 '08 19:08

Jason Z


People also ask

How do you implement a circular shift in C?

Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10, after circular shift y = 5, z = 8, x = 10. Call the function with variables a, b, c to circularly shift values.

How do you do a circular right shift?

1 x: Right Circular Shift the array x times. If an array is a[0], a[1], …., a[n – 1], then after one right circular shift the array will become a[n – 1], a[0], a[1], …., a[n – 2]. 2 y: Left Circular Shift the array y times.

Is bit shifting circular?

In computer programming, a bitwise rotation, also known as a circular shift, is a bitwise operation that shifts all bits of its operand. Unlike an arithmetic shift, a circular shift does not preserve a number's sign bit or distinguish a floating-point number's exponent from its significand.

How do you rotate a bit?

Bit rotation is an operation similar to shift except that the bits that fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at the right end are put back at the left end.


3 Answers

If you know the size of type, you could do something like:

uint i = 17;
uint j = i << 1 | i >> 31;

... which would perform a circular shift of a 32 bit value.

As a generalization to circular shift left n bits, on a b bit variable:

/*some unsigned numeric type*/ input = 17;
var result = input  << n | input  >> (b - n);


@The comment, it appears that C# does treat the high bit of signed values differently. I found some info on this here. I also changed the example to use a uint.
like image 114
Chris Marasti-Georg Avatar answered Sep 22 '22 22:09

Chris Marasti-Georg


One year ago I've to implement MD4 for my undergraduate thesis. Here it is my implementation of circular bit shift using a UInt32.

private UInt32 RotateLeft(UInt32 x, Byte n)
{
      return UInt32((x << n) | (x >> (32 - n)));
}
like image 11
jaircazarin-old-account Avatar answered Sep 21 '22 22:09

jaircazarin-old-account


Sincce .NET Core 3.0 and up there's BitOperations.RotateLeft() and BitOperations.RotateRight() so you can just use something like

BitOperations.RotateRight(12, 3);
BitOperations.RotateLeft(34L, 5);

In previous versions you can use BitRotator.RotateLeft() and BitRotator.RotateRight() in Microsoft.VisualStudio.Utilities

like image 6
phuclv Avatar answered Sep 23 '22 22:09

phuclv