vb.net 如何在十进制数中测试一点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14012765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:40:16  来源:igfitidea点击:

How to test a bit in a decimal number

vb.netbitwise-operatorsboolean-logicboolean-operations

提问by GreenBear

I have a set of decimal numbers. I need to check if a specific bit is set in each of them. If the bit is set, I need to return 1, otherwise return 0.
I am looking for a simple and fast way to do that.
Say, for example, I am checking if the third bit is set. I can do (number AND (2^2)), it will return 4 if the bit is set, otherwise it will return 0. How do I make it to return 1 instead of 4?
Thank you!

我有一组十进制数。我需要检查是否在每个中设置了特定位。如果设置了该位,我需要返回 1,否则返回 0。
我正在寻找一种简单快捷的方法来做到这一点。
比如说,我正在检查是否设置了第三位。我可以做 (number AND (2^2)),如果该位被设置,它会返回 4,否则它会返回 0。如何让它返回 1 而不是 4?
谢谢!

回答by Richard Buckmaster

if ((number AND (2^bitnumber) <> 0) then return 1 else return 0 end if

If you can change your return type to boolean then this is more elegant

如果您可以将返回类型更改为布尔值,那么这会更优雅

return ((number AND (2^bitnumber)) <> 0)

回答by Steven Doggart

While the division solution is a simple one, I would think a bit-shift operation would be more efficient. You'd have to test it to be sure, though. For instance, if you are using 1 based bit indexes, you could do this:

虽然除法解决方案很简单,但我认为位移操作会更有效。不过,您必须对其进行测试才能确定。例如,如果您使用基于 1 的位索引,您可以这样做:

Dim oneOrZero As Integer = (k And 2 ^ (n - 1)) >> (n - 1)

(Where k is the number and n is the bit index). Of, if you are using 0 based bit indexes, you could just do this:

(其中 k 是数字,n 是位索引)。当然,如果您使用基于 0 的位索引,您可以这样做:

Dim oneOrZero As Integer = (k And 2 ^ n) >> n

回答by David BS

I really don't know if it can help anyone more than the above, but, here we go.

我真的不知道它是否可以帮助比上述更多的人,但是,我们走了。

When I need to fast check a bit in number I compare the decimal-value of this bit directly.

当我需要快速检查数字时,我会直接比较该位的十进制值。

I mean, if I would need to see of the 6th bit is on (32), I check its decimal value, like this:

我的意思是,如果我需要看到第 6 位在 (32) 上,我会检查它的十进制值,如下所示:

if x and 32 = 32 then "the bit is ON"

Try for instance check 38 with 32, 4 and 2... and the other bits. You will see only the actual bits turned on.

尝试例如检查 38 与 32、4 和 2... 以及其他位。您将只看到打开的实际位。

I hope it can help.

我希望它能有所帮助。

回答by GreenBear

Sorry, guys, I am too slow today.
To test a bit number "n" in a decimal number "k":
(k AND 2^(n-1))/(2^(n-1))
will return 1 if the bit is set, otherwise will return 0.
=====================================================
Hi again, guys!
I compared the performance of the three proposed solutions with zero-based indexes, and here are the results:
"bit-shift solution" - 8.31 seconds
"if...then solution" - 8.44 seconds
"division solution" - 9.41 seconds
The times are average of the four consecutive runs.
Surprisingly for me, the second solution outperformed the third one.
However, after I modified the "division solution" this way:
p = 2 ^ n : oneOrZero = (k And p) / p
it started to run in 7.48 seconds.
So, this is the fastest of the proposed solutions (despite of what Keith says :-).
Thanks everybody for the help!

对不起,伙计们,我今天太慢了。
要测试十进制数“k”中的位数字“n”:
(k AND 2^(n-1))/(2^(n-1))
如果该位被设置,将返回 1,否则将返回 0 .
================================================== ====
再次嗨,伙计们!
我将三个建议的解决方案与基于零的索引的性能进行了比较,结果如下:
“位移解决方案” - 8.31 秒
“如果...那么解决方案” - 8.44 秒
“除法解决方案” - 9.41 秒
时间是连续四次运行的平均值。
令我惊讶的是,第二个解决方案的表现优于第三个。
但是,在我以这种方式修改“除法解决方案”之后:
p = 2 ^ n : oneOrZero = (k And p) / p
它在 7.48 秒内开始运行。
因此,这是所提出的解决方案中最快的(尽管 Keith 说:-)。
感谢大家的帮助!