确定数字是否在二进制序列中 1 2 4 8 16 32 64 等
可能的重复:
如何检查一个数字是否是 的幂2
我想确定
中是否有一个数字 1 2 4 8 16 32 64 128 256 第512章 1024 2048 4096 8192 16384 ...
我尝试了这个:
public static void Main(string[] args)
{
int result = 1;
for (int i = 0; i < 15; i++)
{
//Console.WriteLine(result);
Console.WriteLine(result % 2);
result *= 2;
}
}
如你所见 它返回 <代码> 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
我应该如何有效地使上面的打印为 0
对于所有这些,包括 1?
Possible Duplicate:
How to check if a number is a power of 2
I want to determine if a number is in
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
...
I tried this:
public static void Main(string[] args)
{
int result = 1;
for (int i = 0; i < 15; i++)
{
//Console.WriteLine(result);
Console.WriteLine(result % 2);
result *= 2;
}
}
As you can see it returns
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...
How should I efficiently make the above print to be 0
for all of them including 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果
i
在您的序列中,则以下表达式应该为 true。(i & (i-1)) == 0)
http://rextester.com/ JRH41036
The following expression should be true if
i
is in your sequence.(i & (i-1)) == 0)
http://rextester.com/JRH41036
像这样的事情怎么样?
这对要检查的数字没有具体限制,但确保如果要检查的数字大于我们试图确定是否在二进制序列中的实际数字,它将停止检查。
How about something like this?
This has no specific limit on the number to check, but makes sure it stops checking if the number to check grows larger than the actual number we're trying to decide if is in the binary sequence.
由于第一次结果是奇数,您将得到
1
,因为之后您将其乘以 2,您将始终得到0
。如果你想获得 2 的幂列表,你需要打印
result
。一个原始的方法是:
Since the first time result is odd, you will get
1
, since right after that you multiply it by 2, you will always get0
.You need to print
result
if you want to get the list of powers of 2.A primitive way to do that will be:
您可以使用以下方法确定数字是否为 2 的幂(包括 2^0):
在这里您可以了解其工作原理和原因。
You can determine if a number is a power of 2 (including 2^0) by using the following method:
Over here you can read why and how this works.
这有点像黑客,但这是有效的......
看起来你实际上是在问数字的二进制表示中是否只有一位是 1
It's a bit of a hack, but this works ...
it seems you're actually asking if only one bit in the binary representation of the number is a 1
您不是测试该数字是否在序列中,而是测试此类数字的生成器...只有打印部分包含某种测试...
尝试使用此代码进行测试:
上面的代码采用命令行参数并根据您发布的标准测试它是否处于二进制序列中...如果是,则打印 1,否则打印 0。
What you is not a test whether the number is in the sequence BUT it is a generator for such numbers... only the print part is containing some sort of a test...
Try this code for a test:
The above code takes a commandline argument and tests it for being in the binary sequence according to the criterion you posted... if so it prints 1, otherwise it prints 0.
没错。 1 0 0 0 0 0 是正确的序列。
第一个循环的结果是 1。 1%2 就是 1。
然后 result *= 2 给出 result 值 2。在下一个循环中运行 2 % 2 = 0。然后 result *= 2 是 4。4%2 是 0。4 *= 2 是 8。8 %2 是 0。因为结果总是乘以 2,它保持在 2 行的幂中,因此 2 结果为 0 的 MOD 运算。所以该代码一切都很好。
Thats correct. 1 0 0 0 0 0 is the correct sequence.
Result is 1 in the first loop. 1 % 2 is 1.
Then result *= 2 gives result the value 2. In the next loop run 2 % 2 = 0. Then result *= 2 is 4. 4%2 is 0. 4 *= 2 is 8. 8 %2 is 0. Since result is always multiplied with 2 it keeps to be in the powers of 2 row and thus als MOD operations with 2 result to 0. So all is fine with that code.
您的代码将仅打印二进制序列。当您应用 MOD 2 时。所以你会得到 0 或 1 。所以它将以二进制序列打印。
your code will print only Binary sequences. as you are applying MOD 2 . so either you will get 0 or 1 . so it will be print in Binary Sequence.