一些常用位运算示例

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

namespace testBinary
{
    class Program
    {
        static int absolute(Int32 a) {
            //return ((a >> 31) & 1) == 1 ? ~a + 1 : a;
            //a >> 31 , 结果 0xFFFFFFFF , 十进制值: -1;
            // a ^ 0xFFFFFFFF (取反,补码)
            // a ^ 0xFFFFFFFF - (-1)  , 值为取反加 1 ;
            return (a ^ (a >> 31)) - (a >> 31);   
            
        }

        static void Main(string[] args)
        {
            //Console.WriteLine(5 >> 31);
            //负数二进制表示为, 原码的补码 + 1
            //正数二进制正是原码.

            //Console.WriteLine(Convert.ToString( -28 >> 2 , 2));
            //Console.WriteLine(Convert.ToString( -28 >> 4 , 2));
            //Console.WriteLine(Convert.ToString(-28 >> 6, 2));

            //按位或
            Console.WriteLine((Convert.ToInt16("100100", 2) | Convert.ToInt16("111", 2)) == Convert.ToInt16("100111", 2));

            uint i = 0, l = 1;

            //判断奇偶性  n = 11
            Console.WriteLine((11 & 1) == 1);

            //取反求模
            Console.WriteLine((~i + i) == uint.MaxValue);

            // i 为无符号 0
            Console.WriteLine(~i);

            //求 -1 的表示形式
            Console.WriteLine( Convert.ToString( ~1 + 1 , 2));

            //负数位运算,右移,高位默认补1
           

            //求模
            Console.WriteLine( Convert.ToString( ~i , 2));

            //求绝对值
            Console.WriteLine("绝对值:{0}", absolute(-5));

            //Console.ReadKey();

            Console.WriteLine(Convert.ToInt16("10010101", 2) & (~0));
            Console.WriteLine(((l << 3) - 1) & Convert.ToInt16("10010101", 2));

            //第k 位取反 , k = 5
            Console.WriteLine(Convert.ToInt16("10010101", 2) ^ (1 << 4));
            Console.WriteLine(Convert.ToInt16("10000101", 2));

            //取末4位
            Console.WriteLine(Convert.ToInt16("10010101", 2) & ((1 << 5) - 1));
            Console.WriteLine(Convert.ToInt16("10101", 2));

            //取右边第k位 k=3
            Console.WriteLine(Convert.ToInt32("10010101", 2) >> 2 & 1);

            //第k 位改成 1 k = 4
            Console.WriteLine(Convert.ToInt32("10010101", 2) | (1 << 5));
            Console.WriteLine(Convert.ToInt32("10110101", 2));

            //把第k 位改成 0 , k = 3
            Console.WriteLine(Convert.ToInt32("10010101", 2) & ~(1 << 2));
            Console.WriteLine(Convert.ToInt32("10010001", 2));

            //末K位取反
            Console.WriteLine(Convert.ToInt32("10010101", 2) ^ ((1 << 3) - 1));
            Console.WriteLine(Convert.ToInt32("10010010", 2));

            //初使化k 位初使位为1 的数
            Console.WriteLine(((UInt32)1 << 16) - 1);
            Console.WriteLine(Convert.ToUInt16("1111111111111111", 2));
            //获取 16位最大值
            Console.WriteLine(0xFFFF);
            Console.WriteLine(Convert.ToString((uint)(~(uint)0), 2));

            //Console.WriteLine( Convert.ToString( ~(uint)5 + (uint)5 , 2));

            //负数以补码表示 , 如 -5 表示为: 11111011
            Console.WriteLine( ((1 << 24) - 1) << 8 | Convert.ToByte("11111011", 2));

            //已知一个补码为11111001,则原码是10000111(-7), 通过位运算证明。
            Console.WriteLine(Convert.ToByte("11111001", 2) | (((1 << 24) - 1) << 8));

            ////求补码 , -7 原码是 00000111 , 取反 ~(00000111) + 1  
            //Console.WriteLine(Convert.ToString(-7, 2));

            //加法运算,包含进位
            Console.WriteLine( Convert.ToString( ~8 + 1 , 2));

            //位运算不包含进位
            Console.WriteLine(Convert.ToString(~8, 2));

            //将末位取反
            Console.WriteLine(Convert.ToString(~8 ^ 1, 2));

            //取第k位值 , k = 3 , 10011110
            Console.WriteLine("第k 位值:{0}" , (158 >> 7 ) & 1);
            Console.WriteLine("第k 位值:{0}", (158 >> 6) & 1);
            Console.WriteLine("第k 位值:{0}", (158 >> 5) & 1);



            Console.ReadKey();
        }
    }
}
原文地址:https://www.cnblogs.com/a_bu/p/5509317.html