Skip to content
Advertisement

How to make tables with X amount of booleans with all possible values?

How do i iterate through all possible X boolean values? i.e i have 2 booleans, how do i make a table with all truths and falses?

like:

  • A 0 0 1 1
  • B 0 1 0 1

Advertisement

Answer

Try to iterate over the 2 ** number of variables integers. The following example for 4 variables:

variables = ['A', 'B', 'C', 'D']                                                                                                                                                                                                                              
lv = len(variables)

print(''.join(variables))
print('=' * lv)
for i in range(2**lv):
    print(f"{i:0{lv}b}")

will produce the following:

ABCD
====
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement