C# 位运算符

C# 位运算符示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int value = 8;
            Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value);
            value = ~value;
            Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value);
            value = ~value;
            Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value);
            Console.ReadLine();
        }
        static string GetIntBinaryString(int n)
    {
       char[] a = new char[15];
       int pos = 14;
       int i = 0;
       while (i < 15)
       {
           if ((n & (1 << i)) != 0)
              a [pos] = '1';
           else
           a[pos] = '0';
           pos--;
           i++;
       }
       return new string(a);
    }
}
    }
日期:2020-04-11 22:50:15 来源:oir作者:oir