public static void Main(string<> args){ int result = 1; for (int i = 0; i As you can see it returns 100000000000000...

You are watching: 1 2 4 8 16 32 64 128

How should I efficiently make the above print to be 0 for all of them including 1?

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?

bool IsInBinarySequence( int number ){ var numbertocheck = 1; doif( number == numbertocheck ) return true;numbertocheck *= 2; while( numbertocheck 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.

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.

You need to print result if you want to get the list of powers of 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; ns

You can determine if a number is a power of 2 (including 2^0) by using the following method:

public bool IsPowerOfTwo(int x) return (x > 0) && ((x & (x - 1)) == 0)Over here you can read why and how this works.

It"s a bit of a hack, but this works ...

static void Main() for (int i = 0; i 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

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.

See more: Why Do American Indians Have Facial Hair, From Beards To Mustaches?

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.

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.

## Not the answer you're looking for? Browse other questions tagged c# .net math binary or ask your own question.

Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.11.1.40614

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.