Programmatically step through every permutation of a 4x4 black and white grid

Hello,

The code provided by @TfGuy44 can be modified to display the value of i instead of a rectangle to see the index of that position:

image

There are other ways to do get the index as well.
See this reference:
https://processing.org/tutorials/pixels

You can then extract the binary bit position (index) of your value (2^16 possible values) using string manipulation or bit manipulation.

Examples:

int i;

void setup(){
  size(400, 400);
  i = 0; 
  frameRate(1); // Slow things down.
}

void draw(){
  // Display 16 bits in decimal, binary and hex
  println(i, hex(i, 4), binary(i, 16), boolean(i)); 
  
  //String manipulation
  String s = binary(i, 16);
  for(int j = 0; j<16; j++)
    {
    print(s.charAt(j));  // Use this with an if() to see if it is a char '1' or '0'
    }
    println();
    
  // Bit manipulation 0 
  for(int j = 0; j<16; j++)
    {
    int check = i&(1<<j);  // Masks everything except the bit being checked
    print(check!=0 ? 1:0); // Check if !=0 otherwise it is 0 and print
    }    
  println();
  
  // Bit manipulation 1
  // Variation of above; you could also change the for loop to decrement
  for(int j = 0; j<16; j++)
    {
    int check = i&(1<<(15-j));
    print(check!=0 ? 1:0);
    }    
  println();
  
  // Bit manipulation 2 
  for(int j = 0; j<16; j++)
    {
    int check = (i>>j)&1; // Shifts the bit to the LSB position and masks everything else
    print(check);         // Print it 
    }    
  println();  
  
  i++;
  println(); 
}

Once you have a handle on how to get the binary value (0 or 1) from that index and you can then use that in your code to modify the grid at that same location.

References:
https://en.wikipedia.org/wiki/Bit_manipulation
https://en.wikipedia.org/wiki/Mask_(computing)
https://en.wikipedia.org/wiki/Bitwise_operation
https://processing.org/reference/#math-bitwise-operators
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html There is a >>> also!

Topic using bit manipulation:
https://discourse.processing.org/t/how-to-do-the-same-as-uint16-t-read-data-8192-in-processing/31891/22

Bit manipulation can be a challenge… at first.
I took the above approach out for a test run and first did this with string manipulation and then progressed to bit manipulation.
This approach worked with my captive audience… strings were easy and bit manipulation took some effort.

This is in C but a good reference:
https://leetcode.com/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently

:)