32 位整数按位与

发布于 2024-08-15 13:32:49 字数 160 浏览 7 评论 0原文

如何在 C# 中对两个 32 位整数执行按位 AND 运算?

相关:

最常见的 C# 位运算。

How do you perform a bitwise AND operation on two 32-bit integers in C#?

Related:

Most common C# bitwise operations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

幻想少年梦 2024-08-22 13:32:49

随着 &操作员

With the & operator

不奢求什么 2024-08-22 13:32:49

使用 & 运算符。

二进制和运算符是为整型类型预定义的[。] 对于整型类型,&计算其操作数的按位与。

来自 MSDN

Use the & operator.

Binary & operators are predefined for the integral types[.] For integral types, & computes the bitwise AND of its operands.

From MSDN.

栖竹 2024-08-22 13:32:49
var x = 1 & 5;
//x will = 1
var x = 1 & 5;
//x will = 1
夏雨凉 2024-08-22 13:32:49
const uint 
  BIT_ONE = 1,
  BIT_TWO = 2,
  BIT_THREE = 4;

uint bits = BIT_ONE + BIT_TWO;

if((bits & BIT_TWO) == BIT_TWO){ /* do thing */ }
const uint 
  BIT_ONE = 1,
  BIT_TWO = 2,
  BIT_THREE = 4;

uint bits = BIT_ONE + BIT_TWO;

if((bits & BIT_TWO) == BIT_TWO){ /* do thing */ }
多孤肩上扛 2024-08-22 13:32:49

使用&运算符(不是 &&)

use & operator (not &&)

烟雨凡馨 2024-08-22 13:32:49
int a = 42;
int b = 21;
int result = a & b;

如需了解更多信息,请查看第一个 Google 结果:
http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx

int a = 42;
int b = 21;
int result = a & b;

For a bit more info here's the first Google result:
http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx

噩梦成真你也成魔 2024-08-22 13:32:49
var result = (UInt32)1 & (UInt32)0x0000000F;

// result == (UInt32)1;
// result.GetType() : System.UInt32

如果您尝试将结果转换为 int,您可能会收到从 0x80000000 开始的溢出错误,
未选中可以避免溢出错误,这种错误在使用位掩码时并不罕见。

result = 0xFFFFFFFF;
Int32 result2;
unchecked
{
 result2 = (Int32)result;
}

// result2 == -1;
var result = (UInt32)1 & (UInt32)0x0000000F;

// result == (UInt32)1;
// result.GetType() : System.UInt32

If you try to cast the result to int, you probably get an overflow error starting from 0x80000000,
Unchecked allows to avoid overflow errors that not so uncommon when working with the bit masks.

result = 0xFFFFFFFF;
Int32 result2;
unchecked
{
 result2 = (Int32)result;
}

// result2 == -1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文