public static void Main(string<> args){ int result = 1; because that (int i = 0; i as you have the right to see it returns 100000000000000...

You are watching: 2 4 8 16 32 64

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

The adhering to expression must be true if ns is in her sequence.

(i & (i-1)) == 0)

http://rextester.com/JRH41036

How around something like this?

bool IsInBinarySequence( int number ){ var numbertocheck = 1; doif( number == numbertocheck ) return true;numbertocheck *= 2; while( numbertocheck This has no certain limit top top the number come check, but makes sure it stops checking if the number to inspect grows bigger than the really number we"re do the efforts to decide if is in the binary sequence.

Since the an initial time result is odd, friend will gain 1, due to the fact that right after that you main point it by 2, friend will always get 0.

You should print result if you want to acquire the list of strength of 2.

Console.WriteLine(result);A primitive means to carry out that will be:

public static void Main(string<> args) int an outcome = 1; int numToCheck = 141234; boolean uncovered = false; because that (int i = 0; ns

You have the right to determine if a number is a strength of 2 (including 2^0) by making use of the following method:

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

It"s a little of a hack, but this functions ...

static void Main() for (int ns = 0; ns c == "1"); Console.ForegroundColor = bitCount == 1 ? ConsoleColor.White : ConsoleColor.DarkGray; Console.WriteLine(i + ": " + (bitCount == 1)); it appears you"re in reality asking if just one bit in the binary depiction of the number is a 1

What friend is no a test whether the number is in the sequence but it is a generator because that such numbers... Only the print part is comprise some type 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) ) an outcome = ((from c in Convert.ToString (numToTest, 2) where c == "1" pick c).Count() == 1 ) ? 1 : 0; Console.WriteLine(result);The above code takes a commandline argument and also tests it for being in the binary succession according to the criterion girlfriend posted... If so the prints 1, otherwise the prints 0.

See more: Which Of The Following Is An Example Of A Polysaccharide ? A) Cellulose

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 an outcome the value 2. In the following loop run 2 % 2 = 0. Then an outcome *= 2 is 4. 4%2 is 0. 4 *= 2 is 8. 8 %2 is 0. Since an outcome is always multiplied with 2 it keeps to be in the strength of 2 row and thus als mode operations with 2 an outcome to 0. So every is fine v that code.

your code will certainly print just Binary sequences. Together you are applying MOD 2 . So one of two people you will acquire 0 or 1 . For this reason it will be publish in Binary Sequence.

## Not the price you're feather for? Browse other questions tagged c# .net mathematics binary or asking your very own question.

easy interview question acquired harder: offered numbers 1..100, find the missing number(s) given specifically k are lacking

site architecture / logo design © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.10.27.40585

her privacy

By clicking “Accept every cookies”, girlfriend agree ridge Exchange can store cookies on your maker and disclose information in accordance v our Cookie Policy.