How to generate all the binary options of N bits in C#

1 Answer

0 votes
using System;
 
class Program
{
    static void generateAllBinaryOptions(int N) {
        int total_bits = 1 << N; // = 2 ^ N
        
        for (int i = 0; i < total_bits; i++) {
            string binary = Convert.ToString(i, 2).PadLeft(N, '0');
             
            Console.WriteLine(binary);
        }
    }
    static void Main() {
        int N = 4;
         
        generateAllBinaryOptions(N);
    }
}
 
 
 
 
 
/*
run:
 
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
 
*/

 



answered Aug 25, 2023 by avibootz
edited Aug 25, 2023 by avibootz

Related questions

1 answer 119 views
1 answer 109 views
1 answer 114 views
1 answer 113 views
1 answer 102 views
1 answer 111 views
...