A bitmask is where we make use of the way computers store an unsigned integer value to create a list of a true/false values.

A typical computer will store an unsigned integer as a 32-bit value. This means we use 32 ‘bits’ to describe a value.

A ‘bit’ is simply a value that can be “on” or “off”, or as you may already be familiar: 0 or 1.

Computers store numbers using these bits. We can make any number from a list 1s and 0s by using a simple technique:

1. We order our bits, remember the index of that bit in our list:

2. For any index (i) that is greater than 1, we perform the operation: 2i, which we’ll call our “binary” value:

3. Wherever we have a “1” in our “bit” value column, we will add the “binary” value to a total:

001011 = 4+16+32 = 52

Bits are always stored right-to-left, so let’s flip the bit-order to be correct and add in some more examples:

If we extend this to 32 bits, we can make large numbers:
00000000000000000000000000000001 = 1
01101010010010001001001110101001 = 1,783,141,289

That’s how we store values – but we can also look at these a bits a different way…

Each bit can also be an entry in our list of “on” and “off” values. For simplicity, we’ll go down to using 4 bits.

Let’s take 4 things in an imaginary 3D scene.

  • Cube
  • Cylinder
  • Sphere
  • Torus

Let us now represent each entry in that list as either a 0 or 1, depending on whether we want to hide them:

Hiding just the Sphere would give you a list of bits as 0010, where the third bit value, “1”, tells us we want to hide the third item in the list: the sphere.

We can then store this bitmask as an integer – a single number. In this case, hiding just the sphere would be an integer value of “4”. If we hid just the Cube, it would be “1”. If we used an integer of “5”, we would hide the Cube and the Sphere!

This is useful as we can pass such an integer value into the shader and work out exactly what vertices to hide.

Categories: Tangents

0 Comments

Leave a Reply

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