RotateLeftメソッド・RotateRightメソッドは、回転シフト(循環シフト、circular shift)を行います。 x86 ROL/ROR命令に類似する演算を行います。
RotateLeft・RotateRightメソッドは、uint
/UInteger
, ulong
/ULong
の整数型のみに対して定義されています。 シフト量(引数offset)は、値の型のビット数(32または64)を超える数を指定できるほか、負数を指定することもできます。 シフト量が負の場合は、逆方向への回転シフトになります。
BitOperations.RotateLeft/RotateRightメソッドで左回転シフト・右回転シフトを行う .NET Core 3.0
using System;
using System.Numerics;
static class Sample {
static void Main()
{
// BitOperations.RotateLeft/RotateRightメソッドはuint/ulongに対してのみ使用できる
uint val = 0b_1000_0000_0000_0000_0000_0000_0000_0001;
// BitOperations.RotateLeftで値に対して左回転シフトを行う
uint val_rol1 = BitOperations.RotateLeft(val, 1);
// BitOperations.RotateRightで値に対して右回転シフトを行う
uint val_ror1 = BitOperations.RotateRight(val, 1);
Console.WriteLine($"RotateLeft({val.ToBinary()}, -1) = {BitOperations.RotateLeft(val, -1).ToBinary()}");
Console.WriteLine($"RotateLeft({val.ToBinary()}, 0) = {BitOperations.RotateLeft(val, 0).ToBinary()}");
Console.WriteLine($"RotateLeft({val.ToBinary()}, 1) = {BitOperations.RotateLeft(val, 1).ToBinary()}");
Console.WriteLine($"RotateLeft({val.ToBinary()}, 2) = {BitOperations.RotateLeft(val, 2).ToBinary()}");
Console.WriteLine($"RotateLeft({val.ToBinary()}, 31) = {BitOperations.RotateLeft(val, 31).ToBinary()}");
Console.WriteLine($"RotateLeft({val.ToBinary()}, 32) = {BitOperations.RotateLeft(val, 32).ToBinary()}");
Console.WriteLine($"RotateLeft({val.ToBinary()}, 33) = {BitOperations.RotateLeft(val, 33).ToBinary()}");
Console.WriteLine();
Console.WriteLine($"RotateRight({val.ToBinary()}, -1) = {BitOperations.RotateRight(val, -1).ToBinary()}");
Console.WriteLine($"RotateRight({val.ToBinary()}, 0) = {BitOperations.RotateRight(val, 0).ToBinary()}");
Console.WriteLine($"RotateRight({val.ToBinary()}, 1) = {BitOperations.RotateRight(val, 1).ToBinary()}");
Console.WriteLine($"RotateRight({val.ToBinary()}, 2) = {BitOperations.RotateRight(val, 2).ToBinary()}");
Console.WriteLine($"RotateRight({val.ToBinary()}, 31) = {BitOperations.RotateRight(val, 31).ToBinary()}");
Console.WriteLine($"RotateRight({val.ToBinary()}, 32) = {BitOperations.RotateRight(val, 32).ToBinary()}");
Console.WriteLine($"RotateRight({val.ToBinary()}, 33) = {BitOperations.RotateRight(val, 33).ToBinary()}");
Console.WriteLine();
}
#region "数値を二進数表記で文字列化するためのメソッド"
static string ToBinary(this uint number) => "0b_" + Convert.ToString(number, 2).PadLeft(8 * sizeof(uint), '0');
#endregion
}
実行結果
RotateLeft(0b_10000000000000000000000000000001, -1) = 0b_11000000000000000000000000000000 RotateLeft(0b_10000000000000000000000000000001, 0) = 0b_10000000000000000000000000000001 RotateLeft(0b_10000000000000000000000000000001, 1) = 0b_00000000000000000000000000000011 RotateLeft(0b_10000000000000000000000000000001, 2) = 0b_00000000000000000000000000000110 RotateLeft(0b_10000000000000000000000000000001, 31) = 0b_11000000000000000000000000000000 RotateLeft(0b_10000000000000000000000000000001, 32) = 0b_10000000000000000000000000000001 RotateLeft(0b_10000000000000000000000000000001, 33) = 0b_00000000000000000000000000000011 RotateRight(0b_10000000000000000000000000000001, -1) = 0b_00000000000000000000000000000011 RotateRight(0b_10000000000000000000000000000001, 0) = 0b_10000000000000000000000000000001 RotateRight(0b_10000000000000000000000000000001, 1) = 0b_11000000000000000000000000000000 RotateRight(0b_10000000000000000000000000000001, 2) = 0b_01100000000000000000000000000000 RotateRight(0b_10000000000000000000000000000001, 31) = 0b_00000000000000000000000000000011 RotateRight(0b_10000000000000000000000000000001, 32) = 0b_10000000000000000000000000000001 RotateRight(0b_10000000000000000000000000000001, 33) = 0b_11000000000000000000000000000000