# Alien Art Drawn With Surprisingly Simple Math

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.

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

## 20 thoughts on “Alien Art Drawn With Surprisingly Simple Math”

1. Nuxi says:

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. irox says:

This makes me want to press play on Conway’s Game of Life. :)

Thanks for the art.

2. Folkert van Heusden says:

I still find z’ = z^2 + c pretty much interesting ;-) (that’s the one for the Mandelbrot fractal)

3. Dave says:

Kinda lookslike taken straight from the copy protection code wheel from Indiana and the Fate of Atlantis.
Ooga booga!

4. Danjovic says:

How can i NFT this ? Lol!!!

5. abjq says:

But what about that pesky patent on the XOR operation?

6. Joe says:

How about posting interesting patterns that we come across…
Here’s one (x | y) % 9)

7. Feinfinger (M-x totally-tame-mode) says:

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.

1. rnjacobs says:

Fortunately the very first reply mentions someone else doing the same. (“bytebeat”)

8. π π says:

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.

9. Mr Name Required says:

Another similarly inspiring computer generated art pattern was this one, ‘Serendipitous Circles’ from BYTE August 1977.
The 6800 assembly code presented there gave a dynamically changing real-time output on a XY scope screen:
https://archive.org/details/byte-magazine-1977-08/page/n71/

10. Jeff day says:

Sorry but I’m new to all this.What does the b stand for? 9 is a number with synchronistic ties to aliens and the paranormal

11. grumbly says:

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?

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.