C# 确定数字是否在二进制序列中 1 2 4 8 16 32 64 等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9259952/
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
Determine if number is in the binary sequence 1 2 4 8 16 32 64 etc
提问by Shimmy Weitzhandler
Possible Duplicate:
How to check if a number is a power of 2
可能的重复:
如何检查一个数字是否是 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
...
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 seeit returns
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...
如您所见,它返回
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 0for all of them including 1?
我应该如何有效地使上述打印0适用于所有人,包括 1?
采纳答案by Sam Greenhalgh
The following expression should be true if iis in your sequence.
如果i在您的序列中,则以下表达式应为真。
(i & (i-1)) == 0)
(i & (i-1)) == 0)
回答by MByD
Since the first time result is odd, you will get 1, since right after that you multiply it by 2, you will always get 0.
因为第一次结果是奇数,你会得到1,因为在那之后你将它乘以 2,你总是会得到0。
You need to print resultif you want to get the list of powers of 2.
result如果您想获得 2 的幂列表,则需要打印。
Console.WriteLine(result);
A primitive way to do that will be:
一种原始的方法是:
public static void Main(string[] args)
{
int result = 1;
int numToCheck = 141234;
boolean found = false;
for (int i = 0; i < 15; i++)
{
if (numToCheck == result) {
found = true;
break;
}
result *= 2;
}
if(found) Console.WriteLine("Awesome");
}
回答by Sebastian P.R. Gingter
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.
那是正确的。1 0 0 0 0 0 是正确的序列。第一个循环中的结果为 1。1 % 2 是 1。然后结果 *= 2 给出结果值 2。在下一个循环中运行 2 % 2 = 0。然后结果 *= 2 是 4。4%2 是 0。4 *= 2 是 8。8 %2 是 0。因为结果总是乘以 2,所以它保持在 2 行的幂,因此 2 结果为 0 的 MOD 操作。所以该代码一切正常。
回答by Yahia
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:
试试这个代码进行测试:
public static void Main(string[] args)
{
int result = 0;
int numToTest = 0;
if ( int.TryParse (args[0], out numToTest) )
{
result = ((from c in Convert.ToString (numToTest, 2) where c == '1' select c).Count() == 1 ) ? 1 : 0;
}
Console.WriteLine(result);
}
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。
回答by Ravi Gadag
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.
您的代码将仅打印二进制序列。当您应用 MOD 2 时。所以要么你会得到 0 要么 1 。所以它将以二进制序列打印。
回答by Dennis Traub
回答by Piotr Justyna
Boolean result = false;
Int32 numberToTest = 64;
Int32 limit = 15;
for (int i = 0; i < limit && !result; i++)
{
if (Math.Pow(2, i).Equals(numberToTest))
{
result = true;
}
}
Console.WriteLine(String.Format("Number {0} {1} a power of 2.", numberToTest, result ? "is" : "is not"));
回答by ?yvind Br?then
How about something like this?
这样的事情怎么样?
bool IsInBinarySequence( int number ){
var numbertocheck = 1;
do{
if( number == numbertocheck ) return true;
numbertocheck *= 2;
}while( numbertocheck <= number );
return false;
}
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.
这对要检查的数字没有特定限制,但确保它停止检查要检查的数字是否大于我们试图确定是否在二进制序列中的实际数字。
回答by Antony Scott
It's a bit of a hack, but this works ...
这有点黑客,但这有效......
static void Main()
{
for (int i = 0; i < 40; i++)
{
var str = Convert.ToString(i, 2);
var bitCount = str.Count(c => c == '1');
Console.ForegroundColor = bitCount == 1 ? ConsoleColor.White : ConsoleColor.DarkGray;
Console.WriteLine(i + ": " + (bitCount == 1));
}
}
it seems you're actually asking if only one bit in the binary representation of the number is a 1
看来您实际上是在问数字的二进制表示中是否只有一位是 1

