位运算,位移,窗体
//BitMove.cs
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace BitArray.Algo { 12 public partial class BitMove : Form { 13 public BitMove() { //构造器 14 InitializeComponent(); 15 } 16 // Windows generated code omitted 17 private StringBuilder ConvertBits(int val) { //二进制转化器 18 int bitMask = 1 << 31; 19 StringBuilder bitBuffer = new StringBuilder(35); 20 for (int i = 1; i <= 32; i++) { 21 if ((val & bitMask) == 0) 22 bitBuffer.Append("0"); 23 else 24 bitBuffer.Append("1"); 25 val <<= 1; 26 if ((i % 8) == 0) 27 bitBuffer.Append(" "); 28 } 29 return bitBuffer; 30 } //二进制转化器 31 private void btnLeft_Click(object sender, EventArgs e) { 32 //控件位置提醒Integer to shift: txtInt1 33 //控件位置提醒Bit to shift: txtBitShift 34 //lblOrigBits 是line 1 的32位 二进制数 35 //lblInt1Bits 是line 2 的32位 二进制数 36 int value = Int32.Parse(txtInt1.Text); 37 lblOrigBits.Text = ConvertBits(value).ToString(); //二进制 输出 38 value <<= Int32.Parse(txtBitShift.Text); //shift移动 输入框 输入的 位数 39 lblInt1Bits.Text = ConvertBits(value).ToString(); //二进制 输出 40 } 41 42 private void btnRight_Click(object sender, EventArgs e) { 43 int value = Int32.Parse(txtInt1.Text); 44 lblOrigBits.Text = ConvertBits(value).ToString(); //二进制 输出 45 value >>= Int32.Parse(txtBitShift.Text); //shift移动 输入框 输入的 位数 46 lblInt1Bits.Text = ConvertBits(value).ToString(); //二进制 输出 47 } 48 49 private void btnClear_Click(object sender, EventArgs e) { 50 txtInt1.Text = ""; 51 txtInt1.Text = ""; 52 lblOrigBits.Text = ""; 53 lblOrigBits.Text = ""; 54 txtInt1.Focus(); 55 } 56 }//public partial class BitMove : Form 57 }//namespace BitArray.Algo