📜  C＃按位和移位运算符

📅  最后修改于: 2020-10-06 09:24:38             🧑  作者: Mango

###### 在本教程中，我们将详细了解C＃中的按位和移位运算符 。 C＃提供4位和2位移位运算符。

List of C# Bitwise Operators
Operator Operator Name
~ Bitwise Complement
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR (XOR)
<< Bitwise Left Shift
>> Bitwise Right Shift

##### 按位或

``````14 = 00001110 (In Binary)
11 = 00001011 (In Binary)``````

14至11之间的按位`OR`运算：

``````00001110
00001011
--------
00001111 = 15 (In Decimal)``````

### 示例1：按位或

``````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);
}
}
}
``````

``14 | 11 = 15``

##### 按位与

``````14 = 00001110 (In Binary)
11 = 00001011 (In Binary)``````

14至11之间的按位AND运算：

``````00001110
00001011
--------
00001010 = 10 (In Decimal)``````

### 示例2：按位与

``````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);
}
}
}
``````

``14 & 11 = 10``

##### 按位异或

``````14 = 00001110 (In Binary)
11 = 00001011 (In Binary)``````

14至11之间的按位XOR运算：

``````00001110
00001011
--------
00000101 = 5 (In Decimal)``````

### 示例3：按位异或

``````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);
}
}
}
``````

``14 ^ 11 = 5``

##### 按位补码

``26 = 00011010 (In Binary)``

26的按位补码运算：

``~ 00011010 = 11100101 = 229 (In Decimal)``

### 示例4：按位补码

``````using System;

namespace Operator
{
class BitWiseComplement
{
public static void Main(string[] args)
{
int number = 26, result;
result = ~number;
Console.WriteLine("~{0} = {1}", number, result);
}
}
}
``````

``~26 = -27``

2’s complement
Decimal Binary 2’s Complement
0 00000000 -(11111111 + 1) = -00000000 = -0 (In Decimal)
1 00000001 -(11111110 + 1) = -11111111 = -256 (In Decimal)
229 11100101 -(00011010 + 1) = -00011011 = -27

`26`的按位补码是229(十进制)， `229`的2的补码是`-27` 。因此，输出是`-27`而不是`229`

##### 按位左移

``num * 2bits``

``42 = 101010 (In Binary)``

42的按位提升移位操作：

``````42 << 1 = 84 (In binary 1010100)
42 << 2 = 168 (In binary 10101000)
42 << 4 = 672 (In binary 1010100000)``````

### 示例5：按位左移

``````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);
}
}
}
``````

``````42<<1 = 84
42<<2 = 168
42<<4 = 672``````

##### 按位右移

``floor(num / 2bits)``

``42 = 101010 (In Binary)``

42的按位提升移位操作：

``````42 >> 1 = 21 (In binary 010101)
42 >> 2 = 10 (In binary 001010)
42 >> 4 = 2 (In binary 000010)``````

### 示例6：按位右移

``````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);
}
}
}
``````

``````42>>1 = 21
42>>2 = 10
42>>4 = 2``````