原文: https://www.programiz.com/csharp-programming/bitwise-operators
按位和移位运算符用于对整数(int,long等)和布尔数据执行位级运算。 这些运算符在现实生活中并不常用。
如果您有兴趣探索更多内容,请访问按位运算的实际应用。
下面列出了 C# 中可用的按位和移位运算符。
C# 按位运算符列表
| 运算符 | 运算符名称 |
|---|---|
~ |
按位补码 |
& |
按位与 |
| |
按位或 |
^ |
按位异或(XOR) |
<< |
按位左移 |
>> |
按位右移 |
|表示按位或运算符。 它对两个操作数的相应位执行按位或运算。 如果任一位为1,则结果为1。 否则结果为0。
如果操作数的类型为bool,则按位或运算等效于它们之间的逻辑或运算。
例如,
14 = 00001110 (In Binary)
11 = 00001011 (In Binary)在 14 至 11 之间按位OR操作:
00001110
00001011
--------
00001111 = 15 (In Decimal)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&表示按位与运算符。 它对两个操作数的相应位执行按位与运算。 如果任一位为0,则结果为0。 否则结果为1。
如果操作数的类型为bool,则按位与运算等效于它们之间的逻辑与运算。
For Example,
14 = 00001110 (In Binary)
11 = 00001011 (In Binary)14 至 11 之间的按位与运算:
00001110
00001011
--------
00001010 = 10 (In Decimal)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按位异或运算符由^表示。 它对两个操作数的相应位执行按位异或操作。 如果相应位相同,则结果为0。 如果相应位不同,则结果为1。
如果操作数的类型为bool,则按位异或运算等效于它们之间的逻辑异或运算。
For Example,
14 = 00001110 (In Binary)
11 = 00001011 (In Binary)14 至 11 之间的按位异或运算:
00001110
00001011
--------
00000101 = 5 (In Decimal)如果您想进一步了解按位异或的用法,请访问异或的魔力
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按位补码运算符由~表示。 它是一元运算符,即仅对一个操作数进行运算。~运算符将每个位取反,即将 1 更改为 0,将 0 更改为 1。
For Example,
26 = 00011010 (In Binary)26 的按位补码运算:
~ 00011010 = 11100101 = 229 (In Decimal)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当我们期望229时,得到-27作为输出。 为什么会发生这种情况?
发生这种情况是因为我们期望是229的二进制值11100101实际上是-27的 2 的补码表示。 计算机中的负数以 2 的补码表示形式表示。
对于任何整数n,n的 2 的补码将为-(n+1)。
2 的补码
| 小数 | 二进制 | 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。
按位左移运算符由<<表示。<<运算符将数字左移指定的位数。 零添加到最低有效位。
以十进制表示,相当于
num * 2bitsFor Example,
42 = 101010 (In Binary)42 的按位提升移位操作:
42 << 1 = 84 (In binary 1010100)
42 << 2 = 168 (In binary 10101000)
42 << 4 = 672 (In binary 1010100000)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按位左移运算符由>>表示。>>运算符将数字向右移动指定的位数。 第一个操作数向右移动第二个操作数指定的位数。
In decimal, it is equivalent to
floor(num / 2bits)For Example,
42 = 101010 (In Binary)Bitwise Lift Shift operation on 42:
42 >> 1 = 21 (In binary 010101)
42 >> 2 = 10 (In binary 001010)
42 >> 4 = 2 (In binary 000010)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