SoFunction
Updated on 2025-03-07

Summary of bit operations in C#

public class BitAction
{
    public static void Main(string[] args)
    {
        int[] power = new int[] { 1, 2, 4, 8, 16, 32, 64 };
        int value = 126;
        /*
* 1's binary form: 00000001
* 2's binary form: 00000010
* 4's binary form: 00000100
* 8's binary form: 00001000
* 16's binary form: 00010000
* 32's binary form: 00100000
* 64's binary form: 01000000
* 126's binary form: 01111110
         */
        for (int i = 0; i < ; i++)
        {
            if ((value & power[i]) != 0)
            {
("There are permissions represented by power[{0}]={1}", i, power[i]);
            }
        }
("Bitwise and: 126&4={0}", value & 4);
("Bitwise or: 126|4={0}", value | 4);
("Transfer left: 126<<4={0}", value << 4);
("Move right: 126>>4={0}", value >> 4);
("XIIOR: 126^4={0}", value ^ 4);
("Bitwise inverse: ~126={0}", ~value);
        ();
    }
}