如何检查 C# 中是否设置了特定位

发布于 2024-12-14 08:09:22 字数 772 浏览 0 评论 0原文

在 C# 中,我有一个 32 位值,我将其存储在 int 中。我需要查看是否设置了特定位。我需要的位是0x00010000

我想出了这个解决方案:

这就是我正在寻找的:

Hex:       0    0    0    1     0    0    0    0    0 
Binary   0000|0000|0000|0001|0000|0000|0000|0000|0000

所以我向右位移 16,这会给我:

Hex:       0    0    0    0     0    0    0    0    1
Binary   0000|0000|0000|0000|0000|0000|0000|0000|0001

然后我向左位移 3,这会给我:

Hex:       0    0    0    0     0    0    0    0   8 
Binary   0000|0000|0000|0000|0000|0000|0000|0000|1000

然后我将 32 位值转换为一个字节,并查看它是否等于 8。

所以我的代码将是这样的:

int value = 0x102F1032;
value = value >> 16;
byte bits = (byte)value << 3;
bits == 8 ? true : false;

是否有一种更简单的方法来检查是否设置了特定位而不进行所有移位?

In C#, I have a 32 bit value which I am storing in an int. I need to see if a particular bit is set. The bit I need is 0x00010000.

I came up with this solution:

Here is what I am looking for:

Hex:       0    0    0    1     0    0    0    0    0 
Binary   0000|0000|0000|0001|0000|0000|0000|0000|0000

So I right bit shift 16, which would give me:

Hex:       0    0    0    0     0    0    0    0    1
Binary   0000|0000|0000|0000|0000|0000|0000|0000|0001

I then bit shift left 3, which would give me:

Hex:       0    0    0    0     0    0    0    0   8 
Binary   0000|0000|0000|0000|0000|0000|0000|0000|1000

I then case my 32 bit value to a byte, and see if it equals 8.

So my code would be something like this:

int value = 0x102F1032;
value = value >> 16;
byte bits = (byte)value << 3;
bits == 8 ? true : false;

Is there a simpler way to check if a particular bit is set without all the shifting?

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

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

发布评论

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

评论(9

烟燃烟灭 2024-12-21 08:09:23

如果你不喜欢位掩码方法:(

int data = 0;

var bits = new BitArray(new int[] { data });

bits.Get(21);

我可能得到了错误的计数,它不是你正在寻找的 21)

对于只有 32 位的情况,这可能有点滥用,但如果你需要使用更长的位数组,这会更清晰。

And if you don't like the bit mask approach:

int data = 0;

var bits = new BitArray(new int[] { data });

bits.Get(21);

(I may have got the wrong count and it's not 21 you are looking for)

This might me a bit abusive for a case where you only have 32 bits, but if you need to work with longer bit arrays this is much clearer.

千と千尋 2024-12-21 08:09:23

你可以说:

if( (number & 0x00010000 ) != 0 )
{
  //the bit is set...
}

you can say:

if( (number & 0x00010000 ) != 0 )
{
  //the bit is set...
}
药祭#氼 2024-12-21 08:09:23
int someInt = 8;
int BitToTest = 3;
bool isSet = (someInt & (1 << BitToTest)) != 0;

如果答案非零,则将其与移位后的值一起设置位。
如果您要进行大量操作,请使用常量 (1 << BitToTest),如果进行很多操作但位不同,则使用静态数组来查找 2 ^ BitToTest。

int someInt = 8;
int BitToTest = 3;
bool isSet = (someInt & (1 << BitToTest)) != 0;

And it with the shifted value, bit is set if the answer is nonzero.
If you are doing one bit a lot use a constant for (1 << BitToTest), if a lot but different bits, a static array to look up 2 ^ BitToTest.

风尘浪孓 2024-12-21 08:09:23

此外,但也许不是更好,您可以使用 BitVector32 结构。

int value =  0x102F1032;
var vector = new BitVector32(value);
return vector[0x1000]; //true

Additionally, but maybe not better you can use the BitVector32 structure.

int value =  0x102F1032;
var vector = new BitVector32(value);
return vector[0x1000]; //true
野鹿林 2024-12-21 08:09:22

您可以使用按位 &操作员:

int value = 0x102F1032;
int checkBit = 0x00010000;
bool hasBit = (value & checkBit) == checkBit;

You can use the bitwise & operator:

int value = 0x102F1032;
int checkBit = 0x00010000;
bool hasBit = (value & checkBit) == checkBit;
手心的海 2024-12-21 08:09:22

这比那容易得多。只需使用按位 AND 运算符,如下所示

(value & 0x00010000) != 0

It's much easier than that. Just use the bitwise AND operator like this

(value & 0x00010000) != 0
捂风挽笑 2024-12-21 08:09:22

你可以像这样检查:

bool bitSet = (value & 0x10000) == 0x10000;

You can just check like so:

bool bitSet = (value & 0x10000) == 0x10000;
夏天碎花小短裙 2024-12-21 08:09:22

您只需执行按位与即可。

int result = yourByte & 16;
if (result != 0)
{
    // do what you need to when that bit is set
}

You can just do a bitwise AND.

int result = yourByte & 16;
if (result != 0)
{
    // do what you need to when that bit is set
}
︶葆Ⅱㄣ 2024-12-21 08:09:22

使用按位与运算符&

return (value & 0x100000) != 0;

Use the bitwise and operator &:

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