一、理解位運算
要學會位運算,首先要清楚什麼是位運算?程序中的所有內容在計算機內存中都是以二進制的形式儲存的(即: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進制數:00000,00001,00010,00011,00100,00101,00110,00111,01000,01001,01010,01011,01100,01101,01110,01111,10000,10001,10010,10011,10100 (每位滿2進1,同時低位補0)
8進制數:00,01,02,03,04,05,06,07,10,11,12,13,14,15,16,17,20,21,22,23,24 (每位滿8進1,同時低位補0)
16進制數:0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14 (每位滿16進1,10~15由A~F字母表示,同時低位補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
16進制【0x2A】轉成2、8、10進制結果:101010、52、42
三、初識位運算(位與與位或運算)
本文一開始就說明了,位運算就是二進制每位數字的運算操作,下面通過代碼示例來初識位運算
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
b0 | b1 = 0 | 1 = 0 | 1 = 10進制:1 或 2進制:1
b2 & b3 = 2 & 4 = 010 & 100 = 10進制:0 或 2進制:000
b2 | b3 = 2 | 4 = 010 | 100 = 10進制:6 或 2進制:110
b4 & b5 = 8 & 16 = 01000 & 10000 = 10進制:0 或 2進制:00000
b4 | b5 = 8 | 16 = 01000 | 10000 = 10進制:24 或 2進制:11000
位與運算:
參加運算的兩個數字,按二進制進行與運算,如果兩個相應的二進位數為1,則該位的結果為 1, 否則為 0 ,即:
0 & 0 = 0;0 & 1 = 0;1 & 0 = 0;1& 1 = 1
也就是只有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進制數:00000,00001,00010,00011,00100,00101,00110,00111,01000,01001,01010,01011,01100,01101,01110,01111,10000,10001,10010,10011,10100
從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",//說白了講:右移N位就是將二進制數後面刪除N位數(不論0或1) 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
<<位左移(移5位)運算:108 -->> 3456 ; 1101100 -->> 110110000000
>>位右移(移5位)運算:108 -->> 3 ; 1101100 -->> 11
^位異或(異或5)運算:108 ^ 5 -->> 105 ; 1101100 ^ 101-->> 1101001
~非運算:是一個單項運算符,用來對一個二進制按位取反,即將 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
為了便於大家進行各種測試,貼出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",//說白了講:右移N位就是將二進制數後面刪除N位數(不論0或1) 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 } } }