Alien Art Drawn With Surprisingly Simple Math

aemkei's xor patterns

Programmer [aemkei] Tweeted the formula (x ^ y) % 9 alongside code for more “alien art”. But how can a formula as simple as (x ^ y) % 9 result in a complex design? The combination of Bitwise XOR (^) and Modulo (%) generate a repeating pattern that’s still complex enough to satisfy the eye, and it’s ok if that doesn’t sound like an explanation. Bitwise operations are useful when working with memory and shift registers, but also worth learning if you want to drive lines or matrices of LEDs or interpret combinations of multiple switches, or in this case a great way to throw an interesting test pattern up on a new flip-dot display or low-res LED matrix. Are you into it? We are, so let’s jump in.

XOR Truth Table
0b00 0b01 0b10 0b11
0b00 0b00 0b01 0b10 0b11
0b01 0b01 0b00 0b11 0b10
0b10 0b10 0b11 0b00 0b01
0b11 0b11 0b10 0b01 0b00

Bitwise XOR compares each binary digit of the two inputs. The XOR returns a 1 when only one of the two digits is a 1, otherwise, it returns a zero for that position. Let’s say the coordinates were 3, 2. Converted to binary we have 0b11 and 0b10. From this truth table, we can see the most-significant digits are both 1, returning a 0, while only one of the least-significant digits is a 1, so the comparison returns a 1.

Moving onto the %, which is the Modulo operator has nothing to do with percentages. This operator divides two numbers and returns the remainder if any. Take 9 % 5. When dividing 9 by 5, 5 goes in once with a remainder of 4 so 9 % 5 = 4. Now our original formula from the top will draw a black box for every ninth number except that the bitwise XOR throws a wrench into that count, varying how often a number divisible by 9 appears and supplying the complexity necessary for these awesome patterns.

detail of aemkei's xor patterns

What are the most interesting designs can you create in a simple formula?

20 thoughts on “Alien Art Drawn With Surprisingly Simple Math

    1. Me too. I actually wrote some BNF a while ago to randomly generate bytebeat strings. Never produced anything profound, unfortunately.

      Could do something similar for these.

  1. This reminds me of some minimal C programs with lots of % spitting out a waveform that sometimes did sound surprisingly well. A one dimensional sibling of the above?

    …unluckily I haven’t bookmarked it.

  2. Very interesting, this class of pattern generators seem to be a form of numerical moire and there is a huge set of possible results. Generate 4 numeric sequences with any function you like so long as they generate a repeating sequence (even just one that is transformed before reuse), then use them to plot the interaction between x0,y0 and x1,y1 with any transform you like applied to x1,y1 sequences, such as scale or rotate, i.e. a simple matrix operation. Should be able to do it in assembler and compact enough for it to stay in a modern processors top level cache while using instructions intended for parallelization. Worth looking at how this would perform directly on a GPU too given how simple the entire class of pattern code is.

  3. Would like to be able to have a C program on Linux set pixels directly, to support explorations like these. Must be possible yet these days it’s so buried under 3D rendering, it’s been about impossible to find something like SetPixel(int x, int y, int rgb). Any links toward something like that?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.