79

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?

101

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.

  • 33
    Subnet Masks are also pretty common. – Matthieu M. Sep 26 '17 at 13:32
  • 38
    @MatthieuM. but aren't subnet masks also bitmasks? They are used to filter out the network address and the host address. – yoyo_fun Sep 26 '17 at 13:36
  • 22
    @JenniferAnderson Isn't a black-and-white image just a pixel bitmask as well? – Bergi Sep 26 '17 at 14:27
  • 8
    Physical image masks are used in silicon chip production, as part of a technique that evolved from traditional photography and process printing. en.wikipedia.org/wiki/Photolithography – Jander Sep 26 '17 at 16:22
  • 20
    Masking is also an artistic term for protecting one area from change while allowing the change of another. It's often used in painting. That's what Masking Tape is for: so you don't paint on the things that you don't want to paint on while still getting to paint on the things you do want to paint on. – Nate Diamond Sep 26 '17 at 23:22
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.

  • 6
    "A bit mask is used to mask some bits of a bit field while exposing others:" I have never thought of it like this but it does make a lot of sense. Thank you for the explanation :) – yoyo_fun Sep 26 '17 at 12:22
  • 1
    @mouvicel can you please point me to some links about how bit masks were used in electronics with logical gates. What was the purpose, what kind of systems were this operations used in and when did this technique begin? – yoyo_fun Sep 26 '17 at 13:44
  • 4
    to clarify : the initial value is ANDed with the mask, and thus only the mask bits at "1" keeps the original bits from the initial value (as, bitwise, "1 and 1 = 1" and "0 and 1 = 0"), and the other bits of the initial value are set to 0 as (bitwise) "0 and 0 = 0" and "1 and 0 = 0" ) (because "And" says that "only when the first bit AND the 2nd bit are set to 1, is the result a 1. all others results in 0". (OR means: either with the 1st bit it 1 OR the 2nd bit is 1 will the result be 1. etc). see en.wikipedia.org/wiki/… – Olivier Dulac Sep 26 '17 at 15:07
  • 2
    @JenniferAnderson: Apart from logical bit masks actual masks are used in electronics: a solder mask is a stencil (a piece of card) that is used to deposit solder paste onto PCBs. You put the mask onto the PCB, use a squeegee to smear solder paste over the mask then the holes in the mask will let the paste through to accurately cover only the areas you need solder paste (youtube.com/watch?v=EqJN1CTCOQs). In chip manufacturing masks are used to build up or etch structures onto silicon. This use of the word "mask" comes from the printing industry. – slebetman Sep 26 '17 at 16:09
  • 4
    @JenniferAnderson: The use of the word "mask" in English to mean cover something is more general and probably a lot older. For example in English we say perfume can be used to mask bad smell. – slebetman Sep 26 '17 at 16:11
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.

34

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."

  • 12
    I've not heard the term "Painting tape". I assume this is what I'd call "masking tape". – thelem Sep 26 '17 at 15:40
  • 12
    @thelem: The term "masking tape", at least in the US, refers to an easily-torn cloth-based tape. When used for painting, it produces a somewhat "fuzzy" edge. Because such tape is used for many other purposes, newer tapes which are better for painting (but are more expensive, and may be less suitable for other purposes) are called "painter's tape". – supercat Sep 26 '17 at 16:14
  • 2
    @Barmar Anyone who's gone to the painting section of the hardware store will have run across them. – chrylis Sep 26 '17 at 17:08
  • 5
    I'm no painter and I know that painter's tape and masking tape are two very different types of tape. Perhaps just saying that "Masking tape is a type of tape that is used for its opaqueness and is easy to write on." – valbaca Sep 26 '17 at 21:26
  • 2
    I tried searching the local (Australia) giant hardware store's web site and the first hit for "painting tape" is a product labeled as "masking tape", so I guess that's that. ;) Good old terminology differences between regions, I suspect. People are really picky about what counts as gaffer tape as well, depending on where they live. – Trejkaz Sep 26 '17 at 23:48
10

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

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).

  • 2
    A bitmap of flag values is still an appropriate and more efficient way to pass around a set of flags than an array of bool or some other way of having each flag in a separate byte. That makes it possible to efficiently do things like if (x & (FLAG_A | FLAG_B)) instead of if (xflags[FLAG_A] || xflags[FLAG_B]). Especially if the mask isn't a constant; being able to pass a mask as an integer is much cheaper than passing a list of flags to be checked. So even if memory and cache were unlimited, it would still be much more efficient to use bitsets and masks in some cases. – Peter Cordes Sep 26 '17 at 18:12
  • Never said it wasn't still useful. Just said the origins had their roots in dealing with hardware registers and limited memory. I still use bit masks when they are appropriate to the problem. – Berin Loritsch Sep 26 '17 at 18:58
  • 1
    What I meant to say was that the origins are probably equal parts memory and performance on old slow computers. – Peter Cordes Sep 26 '17 at 19:00
  • 1
    Agreed. Having programmed a Commodore 64, you also needed to know bit masks to do things with graphics, sound, serial and parallel I/O. The controller chips were mapped to memory addresses with pins mapped to bits within that address. I guess you could argue whether the bit mask influenced the hardware interface or vice versa. Either way, you had to know them to get anything useful done. – Berin Loritsch Sep 26 '17 at 19:09
  • @BerinLoritsch Thank you for the answer. Could you explain what do you mean by "Hardware registers were mapped to a contiguous set of bits" ? Aren't hardware registers accessed independently of each other? – yoyo_fun Sep 27 '17 at 7:16
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.

  • +1 for "basic instruction in every binary computer". People need to know what computers are, and why they are that way. – user251748 Sep 28 '17 at 16:36

protected by gnat Sep 27 '17 at 21:38

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.