# C# 按位和移位运算符 > 原文: [https://www.programiz.com/csharp-programming/bitwise-operators](https://www.programiz.com/csharp-programming/bitwise-operators) #### 在本教程中,我们将详细学习 C# 中的按位和移位运算符。 C# 提供了 4 个按位和 2 个移位运算符。 按位和移位运算符用于对整数(`int`,`long`等)和布尔数据执行位级运算。 这些运算符在现实生活中并不常用。 如果您有兴趣探索更多内容,请访问[按位运算的实际应用](https://stackoverflow.com/questions/3883384/practical-applications-of-bitwise-operations "Practical applications of bitwise operators")。 下面列出了 C# 中可用的按位和移位运算符。 List of C# Bitwise Operators | 运算符 | 运算符名称 | | --- | --- | | `~` | 按位补码 | | `&` | 按位与 | | | | 按位或 | | `^` | 按位异或(XOR) | | `<<` | 按位左移 | | `>>` | 按位右移 | * * * ## 按位或 `|`表示按位或运算符。 它对两个操作数的相应位执行按位或运算。 如果任一位为`1`,则结果为`1`。 否则结果为`0`。 如果操作数的类型为`bool`,则按位或运算等效于它们之间的逻辑或运算。 例如, ```cs 14 = 00001110 (In Binary) 11 = 00001011 (In Binary) ``` 在 14 至 11 之间按位`OR`操作: ```cs 00001110 00001011 -------- 00001111 = 15 (In Decimal) ``` ### 示例 1:按位或 ```cs using System; namespace Operator { class BitWiseOR { public static void Main(string[] args) { int firstNumber = 14, secondNumber = 11, result; result = firstNumber | secondNumber; Console.WriteLine("{0} | {1} = {2}", firstNumber, secondNumber, result); } } } ``` 当我们运行程序时,输出将是: ```cs 14 | 11 = 15 ``` * * * ## 按位与 `&`表示按位与运算符。 它对两个操作数的相应位执行按位与运算。 如果任一位为`0`,则结果为`0`。 否则结果为`1`。 如果操作数的类型为`bool`,则按位与运算等效于它们之间的逻辑与运算。 For Example, ```cs 14 = 00001110 (In Binary) 11 = 00001011 (In Binary) ``` 14 至 11 之间的按位与运算: ```cs 00001110 00001011 -------- 00001010 = 10 (In Decimal) ``` ### 示例 2:按位与 ```cs using System; namespace Operator { class BitWiseAND { public static void Main(string[] args) { int firstNumber = 14, secondNumber = 11, result; result = firstNumber & secondNumber; Console.WriteLine("{0} & {1} = {2}", firstNumber, secondNumber, result); } } } ``` 当我们运行程序时,输出将是: ```cs 14 & 11 = 10 ``` * * * ## 按位异或 按位异或运算符由`^`表示。 它对两个操作数的相应位执行按位异或操作。 如果相应位**相同**,则结果为`0`。 如果相应位不同,则结果为`1`。 如果操作数的类型为`bool`,则按位异或运算等效于它们之间的逻辑异或运算。 For Example, ```cs 14 = 00001110 (In Binary) 11 = 00001011 (In Binary) ``` 14 至 11 之间的按位异或运算: ```cs 00001110 00001011 -------- 00000101 = 5 (In Decimal) ``` 如果您想进一步了解按位异或的用法,请访问[异或的魔力](https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html "The Magic of XOR operator") ### 示例 3:按位异或 ```cs using System; namespace Operator { class BitWiseXOR { public static void Main(string[] args) { int firstNumber = 14, secondNumber = 11, result; result = firstNumber^secondNumber; Console.WriteLine("{0} ^ {1} = {2}", firstNumber, secondNumber, result); } } } ``` 当我们运行程序时,输出将是: ```cs 14 ^ 11 = 5 ``` * * * ## 按位补码 按位补码运算符由`~`表示。 它是一元运算符,即仅对一个操作数进行运算。`~`运算符**将每个位**取反,即将 1 更改为 0,将 0 更改为 1。 For Example, ```cs 26 = 00011010 (In Binary) ``` 26 的按位补码运算: ```cs ~ 00011010 = 11100101 = 229 (In Decimal) ``` ### 示例 4:按位补码 ```cs using System; namespace Operator { class BitWiseComplement { public static void Main(string[] args) { int number = 26, result; result = ~number; Console.WriteLine("~{0} = {1}", number, result); } } } ``` 当我们运行程序时,输出将是: ```cs ~26 = -27 ``` 当我们期望`229`时,得到`-27`作为输出。 **为什么会发生这种情况?** 发生这种情况是因为我们期望是`229`的二进制值`11100101`实际上是`-27`的 2 的补码表示。 计算机中的负数以 2 的补码表示形式表示。 对于任何整数`n`,`n`的 2 的补码将为`-(n+1)`。 2's complement | 小数 | 二进制 | 2 的补码 | | --- | --- | --- | | 0 | 00000000 | `-(11111111 +1) = -00000000 = -0`(十进制) | | 1 | 00000001 | `-(11111110 +1) = -11111111 = -256`(十进制) | | 229 | 11100101 | `-(00011010 + 1) = -00011011 = -27` | 溢出值在 2 的补码中被忽略。 `26`的按位补码为 229(十进制),`229`的 2 补码为`-27`。 因此,输出为`-27`而不是`229`。 * * * ## 按位左移 按位左移运算符由`<<`表示。`<<`运算符将数字左移指定的位数。 零添加到最低有效位。 以十进制表示,相当于 ```cs num * 2bits ``` For Example, ```cs 42 = 101010 (In Binary) ``` 42 的按位提升移位操作: ```cs 42 << 1 = 84 (In binary 1010100) 42 << 2 = 168 (In binary 10101000) 42 << 4 = 672 (In binary 1010100000) ``` ### 示例 5:按位左移 ```cs using System; namespace Operator { class LeftShift { public static void Main(string[] args) { int number = 42; Console.WriteLine("{0}<<1 = {1}", number, number<<1); Console.WriteLine("{0}<<2 = {1}", number, number<<2); Console.WriteLine("{0}<<4 = {1}", number, number<<4); } } } ``` 当我们运行程序时,输出将是: ```cs 42<<1 = 84 42<<2 = 168 42<<4 = 672 ``` * * * ## 按位右移 按位左移运算符由`>>`表示。`>>`运算符将数字向右移动指定的位数。 第一个操作数向右移动第二个操作数指定的位数。 In decimal, it is equivalent to ```cs floor(num / 2bits) ``` For Example, ```cs 42 = 101010 (In Binary) ``` Bitwise Lift Shift operation on 42: ```cs 42 >> 1 = 21 (In binary 010101) 42 >> 2 = 10 (In binary 001010) 42 >> 4 = 2 (In binary 000010) ``` ### 示例 6:按位右移 ```cs using System; namespace Operator { class LeftShift { public static void Main(string[] args) { int number = 42; Console.WriteLine("{0}>>1 = {1}", number, number>>1); Console.WriteLine("{0}>>2 = {1}", number, number>>2); Console.WriteLine("{0}>>4 = {1}", number, number>>4); } } } ``` 当我们运行程序时,输出将是: ```cs 42>>1 = 21 42>>2 = 10 42>>4 = 2 ```