Site Map

SDSS-III Bitmasks


SDSS-III makes heavy use of the concept of a bitmask in spectroscopic target flags and other contexts. A bitmask uses the bits in an integer as "toggles" to indicate whether certain conditions are met. For a more general introduction to this concept, consult Wikipedia or other online resources on the subject. The links on the left side tell you details about usage in CAS, the SEGUE survey, and idlutils (for those using IDL to look at the flat files). For the actual bit values for all SDSS-III flags, see the other links on the left-side panel. What follows below is a primer for those who have never used bitmasks before.

Converting between hex, binary, and decimal
0x1 1 1
0x2 10 2
0x4 100 4
0x8 1000 8
0x10 10000 16
0x20 100000 32
0x40 1000000 64
0x8010000000 128

Within SDSS-III, the most common use of a bitmask is to indicate the status of an object (or spectrum, or target, or whatever) with respect to some set of conditions. For example, a given photometric object might be saturated, and be deblended, and have some interpolated pixels. All of these conditions are tracked as "bits" in a bitmask called "flags". For instance, if bit 18 is set, it indicates there is a saturated pixel in the object; if bit 18 is not set, there is no saturated pixel in the object. This sort of bitmask is useful when there are many possible Boolean (true/false) conditions to track, since it doesn't require an individual variable for each one.

In more detail, when we refer above to "bit 18", we are referring to the zero-indexed bit, starting with the least significant. Thus, if only bit 18 is set, the integer is equal to 218=262144. Of course, in general, many bits can be set, so the value of the variable is not necessarily a power of two. If the integer is signed, note that bit 31 indicates the sign of the integer, so the integer value of a bitmask might be interpreted by the computer as negative.

Note also that many people express bitmasks in "hexadecimal" instead of decimal. You can tell when people are doing this because they will start with "0x" as in "0x00000100" instead of "8". The choice to write the numbers in hexadecimal is just a convention (the values in the files and in CAS are regular integers). However, this choice does often make it easier to figure out which bit is being referred to. For example, it is easy to figure out that "0x00040000" is bit 18 than to figure out that 262144 is bit 18. The table above shows examples of converting among hex, binary and decimal numbers.

Be aware that many programming languages provide tools for translating between binary, hex, and decimal (examples for IDL are given in the link at left.


To get a sense of this behavior, consider some one-byte unsigned integers, and what their bits are:

bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 integer value
0 1 0 0 1 0 1 1 = 75
1 0 0 0 0 0 1 0 = 130
0 0 0 0 0 1 1 1 = 7

To check the value of one or more bits, one needs to execute a "bitwise and" on the bitmask. The simplest case is checking the value of a single bit. In C, for example, the bitwise and operator is "&", so you can write an if statement like:

  if((myflag & 4) != 0) {
    printf("Bit 2 is set\n");
  } else {
    printf("Bit 2 is not set\n");

which will output whether or not bit 2 is set.

What is the code doing here? It is asking for each bit, "is this bit set in both myflag and in 4?" If so, the result (an integer) will have that bit set. We can look for example at what this operation would look like if myflag were equal to 13:

bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
myflag = 0 0 0 0 1 1 0 1
4 = 0 0 0 0 0 1 0 0
myflag & 4 = 0 0 0 0 0 1 0 0

Clearly the result equals "4", and so the condition is satisfied: bit 2 is set! You can easily generalize these sort of operations to bitwise "or" or "exclusive or", or "and not". Consult a good computer science reference for those details.

For SDSS-III, the important thing to know is what each bit means for each type of bitmask. The links on the left side will yield tables for each type, telling you what it means when each bit it set.