The binary code of decimal numbers
Bitwise and ( | )
Bitwise or ( & )
Bitmasks
Bitwise operators may be used with Envisat flags to mask unwanted pixels in an image. To create such masks based on the Flag Codings and the Flags data set, it is necessary to understand how these operators work. Where logical operators (AND, OR, NOT) operate on whole numbers, bitwise operators examine the binary code of the number bit by bit, hence the name bitwise.
Decimal (base 10) numbers may be converted to binary, that is to strings of 0s and 1s. For instance 155 is 10011011 as you may see from the table below:
Bit No: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
![]() | ||||||||
2n : | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
![]() | ||||||||
Decimal: | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
![]() | ||||||||
155 = | 128 + | 0 + | 0 + | 16 + | 8 + | 0 + | 2 + | 1 |
![]() | ||||||||
Binary: | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 1 |
Bitwise operators compare two numbers by looking at their value (0 or 1) at each of the binary position (bits). The result is another binary number. There are two binary operators: bitwise OR ( | ) and bitwise AND ( & ).
With bitwise OR the two numbers are compared bit by bit and if either of the two have a 1 in a specific position, the result is a 1 in that position. For example, comparing 155 and 17:
Bit No: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
![]() | ||||||||
155 : | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 1 |
![]() | ||||||||
4 : | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
![]() | ||||||||
155 | 4 : | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 |
The result, as you can see is 159
With bitwise AND both numbers must have a 1 in a specific position for the result to have a 1 in that position. Using 155 and 4 as examples again:
Bit No: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
![]() | ||||||||
155 : | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 1 |
![]() | ||||||||
4 : | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
![]() | ||||||||
155 & 4 : | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
The result is 0 because there was no bit where both numbers contained a 1
Bitwise operators may be used with conditional statements to create masks based on a data set of flags. The image containing the flags is compared to the flag code pixel by pixel to find the pixels that contain that particular flag. This is best illustrated with an example:
The Meris level 1B flag code 'LAND_OCEAN' is 16. A bitmask condition that will identify all land pixels would therefore have the following form:
if ((flags & 16)== 16)
which is true only for pixels that have a 1 in bit 4. Comparing a pixel with the flag value 155 to the flag code 16 gives
Bit No: | - | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
![]() | |||||||||
Flag code: | 16 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
![]() | |||||||||
Flag pixel: | 155 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 1 |
![]() | |||||||||
(155&16) : | 16 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
So the expression (flags & 16) produces 16 as a result. Thus ((flags&16)==16) becomes ((16)==16), which is of course true. We now know that this particular pixel has been assigned the LAND_OCEAN flag.
Note: In MERIS Level 1 data, the interpretation of this particular flag is:
True: The pixel is a land pixel.
False: The pixel is an ocean pixel.