1 二进制数的按位旋转
二进制数的按位旋转(翻转)是编程中常见的按位运算方法。
二进制数的按位旋转分为左转、右转。
左转意味着数据变大,右转意味着数据变小(有损)。
2 源程序
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer.Algorithm
{
public static partial class Algorithm_Gallery
{
public static int Left_Rotate(int n, int d, int INT_BITS = 32)
{
return (n << d) | (n >> (INT_BITS - d));
}
public static int Right_Rotate(int n, int d, int INT_BITS = 32)
{
return (n >> d) | (n << (INT_BITS - d));
}
}
}
POWER BY TRUFFER.CN
BY 315SOFT.COM
3 代码格式
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer.Algorithm
{
public static partial class Algorithm_Gallery
{
public static int Left_Rotate(int n, int d, int INT_BITS = 32)
{
return (n << d) | (n >> (INT_BITS - d));
}
public static int Right_Rotate(int n, int d, int INT_BITS = 32)
{
return (n >> d) | (n << (INT_BITS - d));
}
}
}