在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二、理解数字进制 上面提到了二进制,除了二进制,我们还有很多的进制,下面列举一些常见的进制 10进制数:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 (每位满10进1,同时低位补0) 2进制、8进制、16进制、32进制、64进制等转换成10进制计算方法我得出一个公式:(^表示次方,如:2^2,即2的2次方,8^5即8的5次方) 每位数字转换成10进制时=进制数^(次方)数字索引位(从0开始计算)*数字 计算示例:(注意黑粗体字) 2进制数:10100=2^0*0+2^1*0+2^2*1+2^3*0+2^4*1=0+0+4+0+16=20 8进制数:24=8^0*4+8^1*2=4+16=20 16进制数:0x14(注意0x是用来表示16进制数的意思,不是数字本身的内容)=16^0*4+16^1*1=4+16=20 至于各进制之间的转换,比如:2进制转换成16进制,如果想自己手算,一般都是先转成10进制,然后将数字进行与进制数相除,直到得出余数小于或等于进制数(或0),当然作为程序员的我们,应该使用现有的方法,如下: Convert.ToString(数字,进制数) 如:Convert.ToString(10,2)=01010,Convert.ToString(10,8)=12 ,Convert.ToString(13,16)=0x0d 综合示例如下: int i10 = 68; int i16 = 0x2A; Console.WriteLine("示例一:"); Console.Write("10进制【68】转成2、8、16进制结果:{0}、{1}、{2}\n", Convert.ToString(i10, 2), Convert.ToString(i10, 8), Convert.ToString(i10, 16)); Console.Write("16进制【0x2A】转成2、8、10进制结果:{0}、{1}、{2}\n", Convert.ToString(i16, 2), Convert.ToString(i16, 8), Convert.ToString(i16, 10)); 输出结果: 10进制【68】转成2、8、16进制结果:1000100、104、44 三、初识位运算(位与与位或运算) 本文一开始就说明了,位运算就是二进制每位数字的运算操作,下面通过代码示例来初识位运算 Console.WriteLine("示例二:"); int b0 = 0, b1 = 1, b2 = 2, b3 = 4, b4 = 8, b5 = 16; FormatWrite("b0", "b1", b0, b1, "&"); FormatWrite("b0", "b1", b0, b1, "|"); Console.WriteLine(); FormatWrite("b2", "b3", b2, b3, "&"); FormatWrite("b2", "b3", b2, b3, "|"); Console.WriteLine(); FormatWrite("b4", "b5", b4, b5, "&"); FormatWrite("b4", "b5", b4, b5, "|"); static void FormatWrite(string n1, string n2, int d1, int d2, string opt) { string writeMsg = string.Format("{0} {1} {2}", n1, opt, n2); writeMsg += string.Format(" = {0} {1} {2}", d1, opt, d2); string d1str = Convert.ToString(d1, 2), d2str = Convert.ToString(d2, 2); int maxLen = Math.Max(d1str.Length, d2str.Length); writeMsg += string.Format(" = {0} {1} {2}", d1str.PadLeft(maxLen, '0'), opt, d2str.PadLeft(maxLen, '0')); switch (opt) { case "&": { writeMsg += string.Format(" = 10进制:{0} 或 2进制:{1}", Convert.ToString(d1 & d2, 10), Convert.ToString(d1 & d2, 2).PadLeft(maxLen, '0')); break; } case "|": { writeMsg += string.Format(" = 10进制:{0} 或 2进制:{1}", Convert.ToString(d1 | d2, 10), Convert.ToString(d1 | d2, 2).PadLeft(maxLen, '0')); break; } } Console.WriteLine(writeMsg); } 输出结果: b0 & b1 = 0 & 1 = 0 & 1 = 10进制:0 或 2进制:0 b2 & b3 = 2 & 4 = 010 & 100 = 10进制:0 或 2进制:000 b4 & b5 = 8 & 16 = 01000 & 10000 = 10进制:0 或 2进制:00000 位与运算: 参加运算的两个数字,按二进制进行与运算,如果两个相应的二进位数为1,则该位的结果为 1, 否则为 0 ,即: 也就是只有1 & 1才会得1,否则都为0; 位或运算: 参加运算的两个数字,按二进制进行或运算,如果两个相应的二进位中只要有一个为 1,则该位的结果就为 1,否则为 0 ,即: 0|0=0; 0|1=1; 1|0=1; 1|1=1; 也就是只有0 & 0才会得0,否则都为1; 四、寻找规律 我们先看一下10进制及2进制0~20的数字 10进制数:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 从2进制数0~20中,我们发现只要是2的偶数次方时,则位数发生变化,且多位中只有一个为1,其余位均为0,找出的数字如下: 00000、00001、00010、,00100、01000、10000 对应10进制数:0、1、2、4、8、16 如果对这些数全部进行位或运算,则最终的结果是:11111,即5位会部是1 从这里发现了什么呢?我是看出来了,不知道各位看官是否看出规律,其实很简单,如果我们把每一位都当成一个控制开关或者说是存在不存在,那么0表示关或者不存在,1表示开或者存在,那么我们可以针对这个规律实现复杂的组合控制。 其实微软早就应用了这个规律特性,比如: typeof(Program).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static); BindingFlags定义如下: 如上,每一个枚举项都是2的次方,每一项都是上一项的2倍,我们也可以利用这个规律实现类似的处理。 五、实现组合控制 Console.WriteLine("示例四:"); ButtonStyle userbtnStyle = ButtonStyle.OK | ButtonStyle.Cancel | ButtonStyle.Alert | ButtonStyle.Info;//用户需要显示的ButtonStyle,通过或运算组合在一起,得出2进制值:1111 string buttonStyleStr = null; //进行位逻辑判断,能够准确识别userbtnStyle的组合的内容 if ((userbtnStyle & ButtonStyle.AlertInfo) == ButtonStyle.AlertInfo) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.AlertInfo); } else if ((userbtnStyle & ButtonStyle.Alert) == ButtonStyle.Alert) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.Alert); } else if ((userbtnStyle & ButtonStyle.Info) == ButtonStyle.Info) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.Info); } if ((userbtnStyle & ButtonStyle.OKCancel) == ButtonStyle.OKCancel) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.OKCancel); } else if ((userbtnStyle & ButtonStyle.OK) == ButtonStyle.OK) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.OK); } else if ((userbtnStyle & ButtonStyle.Cancel) == ButtonStyle.Cancel) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.Cancel); } Console.WriteLine("需要显示的按钮有:" + buttonStyleStr.Substring(1)); enum ButtonStyle { None = 0x00, OK = 0x01, Cancel = 0x02, Alert = 0x04, Info = 0x08, OKCancel = 0x01 | 0x02, AlertInfo = 0x04 | 0x08 } 输出结果: 需要显示的按钮有:AlertInfo+OKCancel 如果改变userbtnStyle的组合,得到的结果也会不同 另外一个示例: Console.WriteLine("示例五:"); AllowType userPermission = AllowType.Add | AllowType.Update | AllowType.Upload | AllowType.Download | AllowType.Select; string userPermissionStr = null; if ((userPermission & AllowType.Edit) == AllowType.Edit) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Edit); } else { if ((userPermission & AllowType.Add) == AllowType.Add) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Add); } if ((userPermission & AllowType.Update) == AllowType.Update) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Update); } if ((userPermission & AllowType.Delete) == AllowType.Delete) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Delete); } if ((userPermission & AllowType.Upload) == AllowType.Upload) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Upload); } } if ((userPermission & AllowType.Read) == AllowType.Read) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Read); } else { if ((userPermission & AllowType.Select) == AllowType.Select) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Select); } if ((userPermission & AllowType.Download) == AllowType.Download) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Download); } } Console.WriteLine("用户具备的权限有:" + userPermissionStr.Substring(1)); enum AllowType { None = 0, Add = 1, Update = 2, Delete = 4, Select = 8, Upload = 16, Download = 32, Edit = Add | Update | Delete | Upload, Read = Select | Download } 输出结果: 用户具备的权限有:Add+Update+Upload+Read 如果改变userPermission的组合,得到的结果也会不同 上述两个例子,就是允分利用位或位与运算,大家可以理解位或是将两者拼在一起,位与是找出组合中是否有包含的部份 六、了解其它位运算 Console.WriteLine("示例六:"); int x1 = 108; Console.Write("~位非运算:{0} -->> {1} ; {2} -->> {3}\n", Convert.ToString(x1, 10), Convert.ToString(~x1, 10), Convert.ToString(x1, 2), Convert.ToString(~x1, 2)); Console.Write("<<位左移(移5位)运算:{0} -->> {1} ; {2} -->> {3}\n",//说白了讲:左移N位就是将二进制数后面补N位0 Convert.ToString(x1, 10), Convert.ToString(x1 << 5, 10), Convert.ToString(x1, 2), Convert.ToString(x1 << 5, 2)); Console.Write(">>位右移(移5位)运算:{0} -->> {1} ; {2} -->> {3}\n",//说白了讲:右移N位就是将二进制数后面删除N位数(不论0或1) Convert.ToString(x1, 10), Convert.ToString(x1 >> 5, 10), Convert.ToString(x1, 2), Convert.ToString(x1 >> 5, 2)); Console.Write("^位异或(异或5)运算:{0} ^ {1} -->> {2} ; {3} ^ {4}-->> {5}\n", Convert.ToString(x1, 10),Convert.ToString(5, 10), Convert.ToString(x1 ^ 5, 10), Convert.ToString(x1, 2), Convert.ToString(5, 2), Convert.ToString(x1 ^ 5, 2)); 输出结果: ~位非运算:108 -->> -109 ; 1101100 -->> 11111111111111111111111110010011
~非运算:是一个单项运算符,用来对一个二进制按位取反,即将 0 变 1,1变 0。 <<左移:用来对一个数每个二进位全部左移若干位,说白了讲:左移N位就是将二进制数后面补N位0 >>右移:用来对一个数每个二进位全部右移若干位,移到右端的低位被舍弃,对无符号数,高位补 0,说白了讲:右移N位就是将二进制数后面删除N位数(不论0或1) ^异或运算: 也称 XOR 运算符。它的规则是若参加运算的两个二进位同号,则结果为0,异号则为1。即 0^0=0; 0^1=1; 1^0=1;1^1=0;说白了讲:若两个都为0,则为0,否则相同的则为0,不相同的则为1
小技巧说明: 大家在定义枚举时,除了0,1外,其余项只需要确保每一项乘以2得出下一项的值即可,如10进制数的值:0,1,2,4,8,16,32,64,128,256,16进制数的值:0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 通过10进制数的值与16进制数的值的对比发现,采用16进制定义枚举值更好,因为我们可以很快速的定义出我们需要的值(都是按照:0,1,2,4,8循环,只是位数上升而矣),比如: 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800,0x1000 对应的10进制数:0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096 明显16进制定义值相对简单一些,我们无需进行*2的计算,当然大家随意选择。
为了便于大家进行各种测试,贴出DEMO代码,供大家学习: using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace TestConsoleApp { class Program { static void Main(string[] args) { Console.SetBufferSize(800, 600); Console.WriteLine("数据进制了解:"); int[] nums = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; Console.WriteLine("10进制数:" + string.Join(",", nums)); Console.WriteLine("2进制数:" + GetFormatNumbersToStr(Convert.ToString, 2, true, nums)); Console.WriteLine("8进制数:" + GetFormatNumbersToStr(Convert.ToString, 8, true, nums)); Console.WriteLine("16进制数:" + GetFormatNumbersToStr(Convert.ToString, 16, true, nums)); Console.WriteLine(); int i10 = 68;//10进制数:68 int i16 = 0x2A;//16进制=16^0*10+16^1*2=10+32=42 -->相当于10进制数:42 Console.WriteLine("示例一:"); Console.Write("10进制【68】转成2、8、16进制结果:{0}、{1}、{2}\n", Convert.ToString(i10, 2), Convert.ToString(i10, 8), Convert.ToString(i10, 16)); Console.Write("16进制【0x2A】转成2、8、10进制结果:{0}、{1}、{2}\n", Convert.ToString(i16, 2), Convert.ToString(i16, 8), Convert.ToString(i16, 10)); Console.WriteLine(); Console.WriteLine("示例二:"); int b0 = 0, b1 = 1, b2 = 2, b3 = 4, b4 = 8, b5 = 16; FormatWrite("b0", "b1", b0, b1, "&"); FormatWrite("b0", "b1", b0, b1, "|"); Console.WriteLine(); FormatWrite("b2", "b3", b2, b3, "&"); FormatWrite("b2", "b3", b2, b3, "|"); Console.WriteLine(); FormatWrite("b4", "b5", b4, b5, "&"); FormatWrite("b4", "b5", b4, b5, "|"); FormatWrite("0~", "~16", 0 | 1 | 2 | 4, 8|16, "|"); Console.WriteLine(); Console.WriteLine("示例三:"); List<int> dds = new List<int>(); int d = 0; while (d <= 500) { if (d < 2) { dds.Add(d); ++d; } else { dds.Add(d); d = d * 2; } } Console.WriteLine("10进制数:" + GetFormatNumbersToStr(Convert.ToString, 10, true, dds.ToArray())); Console.WriteLine("16进制数:" + GetFormatNumbersToStr(Convert.ToString, 16, true, dds.ToArray())); Console.WriteLine("2进制数:" + GetFormatNumbersToStr(Convert.ToString, 2, true, dds.ToArray())); Console.WriteLine(); Console.WriteLine("示例四:"); ButtonStyle userbtnStyle = ButtonStyle.OK | ButtonStyle.Cancel | ButtonStyle.Alert | ButtonStyle.Info;//用户需要显示的ButtonStyle,通过或运算组合在一起,得出2进制值:1111 string buttonStyleStr = null; //进行位逻辑判断,能够准确识别userbtnStyle的组合的内容 if ((userbtnStyle & ButtonStyle.AlertInfo) == ButtonStyle.AlertInfo) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.AlertInfo); } else if ((userbtnStyle & ButtonStyle.Alert) == ButtonStyle.Alert) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.Alert); } else if ((userbtnStyle & ButtonStyle.Info) == ButtonStyle.Info) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.Info); } if ((userbtnStyle & ButtonStyle.OKCancel) == ButtonStyle.OKCancel) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.OKCancel); } else if ((userbtnStyle & ButtonStyle.OK) == ButtonStyle.OK) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.OK); } else if ((userbtnStyle & ButtonStyle.Cancel) == ButtonStyle.Cancel) { buttonStyleStr += "+" + Enum.GetName(typeof(ButtonStyle), ButtonStyle.Cancel); } Console.WriteLine("需要显示的按钮有:" + buttonStyleStr.Substring(1)); Console.WriteLine(); Console.WriteLine("示例五:"); AllowType userPermission = AllowType.Add | AllowType.Update | AllowType.Upload | AllowType.Download | AllowType.Select; string userPermissionStr = null; if ((userPermission & AllowType.Edit) == AllowType.Edit) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Edit); } else { if ((userPermission & AllowType.Add) == AllowType.Add) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Add); } if ((userPermission & AllowType.Update) == AllowType.Update) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Update); } if ((userPermission & AllowType.Delete) == AllowType.Delete) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Delete); } if ((userPermission & AllowType.Upload) == AllowType.Upload) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Upload); } } if ((userPermission & AllowType.Read) == AllowType.Read) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Read); } else { if ((userPermission & AllowType.Select) == AllowType.Select) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Select); } if ((userPermission & AllowType.Download) == AllowType.Download) { userPermissionStr += "+" + Enum.GetName(typeof(AllowType), AllowType.Download); } } Console.WriteLine("用户具备的权限有:" + userPermissionStr.Substring(1)); Console.WriteLine(); Console.WriteLine("示例六:"); int x1 = 108; Console.Write("~位非运算:{0} -->> {1} ; {2} -->> {3}\n", Convert.ToString(x1, 10), Convert.ToString(~x1, 10), Convert.ToString(x1, 2), Convert.ToString(~x1, 2)); Console.Write("<<位左移(移5位)运算:{0} -->> {1} ; {2} -->> {3}\n",//说白了讲:左移N位就是将二进制数后面补N位0 Convert.ToString(x1, 10), Convert.ToString(x1 << 5, 10), Convert.ToString(x1, 2), Convert.ToString(x1 << 5, 2)); Console.Write(">>位右移(移5位)运算:{0} -->> {1} ; {2} -->> {3}\n",//说白了讲:右移N位就是将二进制数后面删除N位数(不论0或1) Convert.ToString(x1, 10), Convert.ToString(x1 >> 5, 10), Convert.ToString(x1, 2), Convert.ToString(x1 >> 5, 2)); Console.Write("^位异或(异或5)运算:{0} ^ {1} -->> {2} ; {3} ^ {4}-->> {5}\n", Convert.ToString(x1, 10),Convert.ToString(5, 10), Convert.ToString(x1 ^ 5, 10), Convert.ToString(x1, 2), Convert.ToString(5, 2), Convert.ToString(x1 ^ 5, 2)); Console.ReadKey(); } static void FormatWrite(string n1, string n2, int d1, int d2, string opt) { string writeMsg = string.Format("{0} {1} {2}", n1, opt, n2); writeMsg += string.Format(" = {0} {1} {2}", d1, opt, d2); string d1str = Convert.ToString(d1, 2), d2str = Convert.ToString(d2, 2); int maxLen = Math.Max(d1str.Length, d2str.Length); writeMsg += string.Format(" = {0} {1} {2}", d1str.PadLeft(maxLen, '0'), opt, d2str.PadLeft(maxLen, '0')); switch (opt) { case "&": { writeMsg += string.Format(" = 10进制:{0} 或 2进制:{1}", Convert.ToString(d1 & d2, 10), Convert.ToString(d1 & d2, 2).PadLeft(maxLen, '0')); break; } case "|": { writeMsg += string.Format(" = 10进制:{0} 或 2进制:{1}", Convert.ToString(d1 | d2, 10), Convert.ToString(d1 | d2, 2).PadLeft(maxLen, '0')); break; } } Console.WriteLine(writeMsg); } static string GetFormatNumbersToStr(Func<int, int, string> ConvertToStringFunc, int toBase, bool showH, params int[] nums) { List<string> strs = nums.Select(n => ConvertToStringFunc(n, toBase)).ToList(); int maxLen = strs.Max(s => s.Length); string strLine = null; foreach (string str in strs) { string str1 = str.PadLeft(maxLen, '0'); if (toBase == 16) str1 = "0x" + str1; if (showH) { strLine += "," + str1; } else { strLine += ",\n" + str1; } } return strLine.Substring(1); } enum ButtonStyle { None = 0x00, OK = 0x01, Cancel = 0x02, Alert = 0x04, Info = 0x08, OKCancel = 0x01 | 0x02, AlertInfo = 0x04 | 0x08 } enum AllowType { None = 0, Add = 1, Update = 2, Delete = 4, Select = 8, Upload = 16, Download = 32, Edit = Add | Update | Delete | Upload, Read = Select | Download } } }
|
请发表评论