Skip to main content

Warm-up shaders · bits · fractals

Sierpinski in one AND

A famous fractal is hiding inside the bits of two coordinates.

Write sierpinski(x, y, size). It is called once for every pixel of a size by size canvas, x running right and y running down from 0 at the top left. Return a brightness: 1 for lit, 0 for dark.

Light a pixel when x and y share no set bits, that is when the bitwise AND x & y is 0. Every other pixel is dark. That single rule draws the Sierpinski triangle, with its right angle at the top left.

Example: 5 is 101 in binary and 2 is 010, so 5 & 2 is 0 and sierpinski(5, 2, 64) returns 1. But 5 & 4 is 4, so sierpinski(5, 4, 64) returns 0.

Tests you can see

  • sierpinski(5, 2, 64)1
  • sierpinski(5, 4, 64)0

More tests are hidden. The server runs them all before a time counts.

Run your code and this canvas paints itself.

javascriptsierpinski()

Tab indents · Cmd/Ctrl+Enter runs