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

后端 未结 6 973
太阳男子
太阳男子 2020-11-29 04:58

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, t

6条回答
  •  难免孤独
    2020-11-29 05:44

    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

提交回复
热议问题