Summary of bit operations in C
- 2020-06-03 08:09:01
- OfStack
A bit operator is an operator that operates on data in base 2. Bitmanipulation is supported by many other languages, such as C, C++, and Java, and C# is no exception. Note that the data types supported by bit operations are basic data types, such as byte, short, char, int, long, etc. C# supports the following types of bit operations:
The & # 8226; Bitwise and
&
The & # 8226; Bitwise or |
The & # 8226; Invert by bit ~
The & # 8226; Shift to the left
<
<
The & # 8226; Moves to the right
>
>
The & # 8226; Exclusive or ^
Bit operation in C # no difference with C bit operation, operation speed is relatively fast, and if the skilled, is relatively easy for you to deal with, especially in 1 related Settings, such as some of authority such as: 1, 2, 4, 8, 16, 32, 64 respectively to view, add, edit, modify, delete, and examination and approval authority value, if a user privilege ultimate authority is a variety of values of the overlay, whether judged by bit operation has certain permissions is quite convenient.
For example:
using System;
public class BitAction
{
public static void Main(string[] args)
{
int[] power = new int[] { 1, 2, 4, 8, 16, 32, 64 };
int value = 126;
/*
* 1 the 2 Base form: 00000001
* 2 the 2 Base form: 00000010
* 4 the 2 Base form: 00000100
* 8 the 2 Base form: 00001000
* 16 the 2 Base form: 00010000
* 32 the 2 Base form: 00100000
* 64 the 2 Base form: 01000000
* 126 the 2 Base form: 01111110
*/
for (int i = 0; i < power.Length; i++)
{
if ((value & power[i]) != 0)
{
Console.WriteLine(" There are power[{0}]={1} The authority represented by ", i, power[i]);
}
}
Console.WriteLine(" Bitwise and: 126&4={0}", value & 4);
Console.WriteLine(" Bitwise or: 126|4={0}", value | 4);
Console.WriteLine(" Left: 126<<4={0}", value << 4);
Console.WriteLine(" Moves to the right: 126>>4={0}", value >> 4);
Console.WriteLine(" Exclusive or: 126^4={0}", value ^ 4);
Console.WriteLine(" Reverse by bit: ~126={0}", ~value);
Console.ReadLine();
}
}