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

**URL:** https://discourse.processing.org/t/programmatically-step-through-every-permutation-of-a-4x4-black-and-white-grid/38133
**Category:** Beginners
**Created:** [July 28, 2022, 4:59am UTC](https://discourse.processing.org/t/programmatically-step-through-every-permutation-of-a-4x4-black-and-white-grid/38133 "2022-07-28T04:59:34Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [August 1, 2022, 3:52pm UTC](https://discourse.processing.org/t/programmatically-step-through-every-permutation-of-a-4x4-black-and-white-grid/38133/6 "2022-08-01T15:52:46Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/3/b33ff535f0468993b3fdd402a64ffcd910cbee75.png)

There are other ways to do get the index as well.  
See this reference:  
[https://processing.org/tutorials/pixels](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:

```auto
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/Bit_manipulation)  
[https://en.wikipedia.org/wiki/Mask\_(computing)](https://en.wikipedia.org/wiki/Mask_(computing))  
[https://en.wikipedia.org/wiki/Bitwise\_operation](https://en.wikipedia.org/wiki/Bitwise_operation)  
[https://processing.org/reference/#math-bitwise-operators](https://processing.org/reference/#math-bitwise-operators)  
[https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html](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](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](https://leetcode.com/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently)

`:)`

---

_[View the full topic](https://discourse.processing.org/t/programmatically-step-through-every-permutation-of-a-4x4-black-and-white-grid/38133)._
