标签:eva 移动 lis generate err long byte ddb 表示
要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作
运算符号
|
意义
|
运算对象类型
|
运算结果类型
|
对象数
|
实例
|
~
|
位逻辑非运算
|
整型,字符型
|
整型
|
1
|
~a
|
&
|
位逻辑与运算
|
2
|
a & b
|
||
|
|
位逻辑或运算
|
2
|
a | b
|
||
^
|
位逻辑异或运算
|
2
|
a ^ b
|
||
<<
|
位左移运算
|
2
|
a<<4
|
||
>>
|
位右移运算
|
2
|
a>>2
|
x<<1= x*2
x<<2= x*4
x<<3= x*8
x<<4= x*16
右移相当于整除. 右移一位相当于除以2;右移两位相当于除以4;右移三位相当于除以8。
x>>1= x/2
x>>2= x/4
x>>3= x/8
x>>4=x/16
位运算帮助类
using System; using System.Linq; namespace BitwiseDemo.Utilities { /// <summary> /// 位运算校验帮助类 /// </summary> public class BitwiseOperationHelper { /// <summary> /// 验证整数是否为2的幂级 /// </summary> /// <remarks></remarks> /// <param name="n"></param> /// <returns></returns> public static bool ValidateValuePower(long n) { return (n & (n - 1)) == 0; } /// <summary> /// 获取2的n次方值 /// </summary> /// <param name="n">n次方值</param> /// <returns></returns> public static long GetValuePower(int n) { return (long)System.Math.Pow(2, n); } /// <summary> /// 获取二进制数 /// </summary> /// <param name="n"></param> /// <returns></returns> public static string GetBinaryValue(long n) { return Convert.ToString(n, 2); } /// <summary> /// 生成位运算数值 /// </summary> /// <param name="list">权限值</param> /// <remarks>每个值执行或操作(value = value | n)</remarks> /// <returns></returns> public static long GenerateBitwise(params long[] list) { if (list == null) throw new Exception("值数组不允许为空"); long value = 0; list.ToList().ForEach(n => { if (!ValidateValuePower(n)) throw new Exception($"值 {n} 为无效的值,不是2的幂级"); if (n < 0 || n > 4611686018427387904) throw new Exception($"鉴权值 {n} 应大于 0 小于 4611686018427387904"); value |= n; }); return value; } /// <summary> /// 添加位值 /// </summary> /// <param name="oldValue">原位数值</param> /// <param name="value">新加入的位值</param> /// <remarks>value = oldValue | newValue</remarks> /// <returns></returns> public static long AddBitwise(long oldValue, long value) { if (!ValidateValuePower(value)) throw new Exception($"值 {value} 为无效的值,不是2的幂级"); if (value < 0 || value > 4611686018427387904) throw new Exception($"值 {value} 应大于 0 小于 4611686018427387904"); long returnValue = oldValue | value; return returnValue; } /// <summary> /// 移除位值 /// </summary> /// <param name="oldValue">鉴权码</param> /// <param name="value">权限值(2的幂级)</param> /// <remarks>code = authCode & (~auth)</remarks> /// <returns></returns> public static long RemoveBitwise(long oldValue, long value) { if (!ValidateValuePower(value)) throw new Exception($"值 {value} 无效"); if (value < 0 || value > 4611686018427387904) throw new Exception($"值 {value} 应在 0 与 4611686018427387904 之间"); long returnValue = oldValue & (~value); return returnValue; } /// <summary> /// 验证位值是否存在 /// </summary> /// <param name="oldValue">原位值</param> /// <param name="value">值(2的幂级)</param> /// <remarks>value == (oldValue & value)</remarks> /// <returns></returns> public static bool ValidateBitwise(long oldValue, long value) { if (!ValidateValuePower(value)) throw new Exception($"值 {value} 为无效的值"); if (oldValue <= 0 || value <= 0) return false; return value == (oldValue & value); } } }
示例:
public void ValidateBitwise() { #region 测试位运算 var validate = BitwiseOperationHelper.ValidateValuePower(3); Console.WriteLine($"validate:{validate}"); //获取枚举值的幂次方 var valuePower = BitwiseOperationHelper.GetValuePower((int)UserRight.Add); Console.WriteLine($"valuePower:{valuePower}"); //获取枚举的二进制数 var binary = BitwiseOperationHelper.GetBinaryValue(2); Console.WriteLine($"binary:{binary}"); var binaryValue = BitwiseOperationHelper.GenerateBitwise(new long[] { (int)UserRight.Add, (int)UserRight.Update, (int)UserRight.View }); var f = BitwiseOperationHelper.ValidateBitwise(binaryValue, (int)UserRight.Delete); var t = BitwiseOperationHelper.ValidateBitwise(binaryValue, (int)UserRight.Update); Console.WriteLine($"f:{f},t:{t}"); #endregion }
使用到的枚举
public enum UserRight { Add = 1 << 0, Update = 1 << 1, View = 1 << 2, Delete = 1 << 3 }
善于总结,才能进步更快
标签:eva 移动 lis generate err long byte ddb 表示
原文地址:https://www.cnblogs.com/yuyoho/p/13185690.html