83

Why are "bit masks" called like this?

I know that they are mainly used for bitwise operations and the usage of bit masks is more efficient than the usage of separate variables.

However my question is why and when were bit masks invented? Were they used since early computing? Are there any other type of "masks" besides bit masks in the IT domain?

Thomas Owens
  • 85,641
  • 18
  • 207
  • 307
yoyo_fun
  • 2,297

7 Answers7

100

A mask (of the facial variety) is something that covers up some parts of your face and lets other parts show through. The terminology is used by analogy in computing: a bitmask covers up (filters out) some bits in a bitset and allows others to pass.

Are there any other type of "masks" besides bit masks in the IT domain?

Just off the top of my head, masks are used frequently in image processing. It's a similar concept: you create a black-and-white image that shows the shape of what to mask off and what to let through.

Mason Wheeler
  • 83,213
55

A bit mask is used to mask some bits of a bit field while exposing others:

initial value: 011011001
bit mask.....: 111110000
result value.: 011010000

This has been used before computing in electronics with logical gates (AND, OR...) or transistors or in electromechanics with relays.

mouviciel
  • 15,491
39

Bitmasks are terribly old. I haven't been able to find a reference to the first one, but they were certainly popular by the advent of 8-bit processors, and likely were also used in 4-bit processors.

The idea behind bitmasks is to take advantage of bitwise parallelism. A 8 bit computer can do the same bitwise operation to 8 bits at once if they're packed into a single native word (which means it fits in a register).

The name comes from masking, which is a general approach to covering up areas you don't want to interact with. For example, consider this stencil for masking off areas of a wall (the stencil has been moved after painting to show the pattern)

Stencils

Masks are also used in photography, where they go by the term "dodge" rather than "stencil." You can use a mask to obscure some of the light during printing to lighten an area.

Photography masks

The term is also used directly in photolithography, which is the technique used to make integrated circuits. The mask prevents light from reaching the photoresist painted on the chip, which creates patterns that later lead to the facinating patterns on the chip. (The below image is one of the masks for the Intel 8080A processor, if you're curious)

Photolithography mask

Likewise, in bit masking, you are selecting the parts of the word you want to operate on, masking off all the rest of the bits. In the example below, I use the "and" operation to mask the input such that only the 3rd, 4th, and 8th bit show through. The rest are "masked" so that they are 0's. The mask I use is 00110001. I show it below with # representing 0 and . representing 1 because that makes the visual appearance of the bitmask similar to that of the physical masks above, and I show a "selected bits" row which shows the bits from the output that were not masked out ("selected bits" is not actually a logical operation that happens... the processor really goes right from input AND mask to output in one step, but I think it clarifies the visual image)

Input          10010111
Mask           ##..###.  (aka 00110001)
-----------------------
(selected)       01   1
Input AND Mask 00010001

As I mentioned, bitmasking is terribly old because it increases productivity of the processor dramatically. On a 4 bit processor, it can make the processor 4x faster. On an 8 bit process, or it can make it 8x faster (on bitwise operations alone, of course).

One fascinating use for this is chess engines. The Chess board has 64 squares. Modern engines have 64 bit integers. This is a terribly convenient bit of luck, so chess engines often leverage it. They have so-called "bitboards" which contain the locations of pieces. This lets you do all sorts of optimizations, such as looking for all pawn moves in a single step.

ferit
  • 193
Cort Ammon
  • 11,917
  • 3
  • 26
  • 35
32

In its most general usage in English, a mask is a device that hides something. Screen printing is mentioned in another answer. Painting tape 'masks off' something to avoid getting paint on it, etc. The Solder Mask on a PC board 'masks off' the area to be soldered from the area not to be soldered.

In the case of "bit masking", some bits are 'hidden' or ignored so that others that are of more interest can be more easily manipulated or simply viewed.

Bit masking is not merely an 'old' technique, it is a primitive operation in most if not all machine instructions, as far as I know from the earliest processors. Typically this is in the form of "use the bit pattern in this register to mask the bits in some other register."

9

A bit mask is similar to screen printing. You select some certain bit position to be taken over into the result:

source value = 42 -> 00101010b
mask = 51 -> 00110011b
result 42&51 = 00100010b -> 34

Another meaning of mask is a page in a graphical user interface where the user can input data.

3

Another kind of physical mask in IT is the lithographic photomask used to etch away only part of a silicon wafer. That wasn’t used to manufacture the earliest computers, but anyone working in the industry in the last fifty years would have been aware of it.

I don’t know when the exact term “bitmask” appeared, but the operation itself is just a bitwise and, which is a basic instruction of every binary computer.

Davislor
  • 1,563
2

Bit masks were invented for a couple of reasons:

  • Hardware registers were mapped to a contiguous set of bits
  • Memory space was very limited in the not too distant past

When you look at how you see the pattern of bits you are ORing to turn on a bit or ANDing to turn off bits, it looks like a mask.

The most common mask (based on bit masks) is an Image mask (see the link I included at the beginning).