I want to use the Landsat QA_PIXEl from this dataset to create a cloud mask in GEE. I only want to mask out the areas where there is high confidence that cloud cover is present. Looking at the documentation

Bits 8-9: Cloud Confidence
0: None
1: Low
2: Medium
3: High

However I do not understand very well the bitwiseAnd function in this context and so I'm not sure if what I'm doing is correct. Which I am guessing is not since I am not giving to the code the information that I only want to mask the High confidence pixels. Plus no image is rendered when I do this.

The code I'm doing for this part is this:

    def maskClouds(image):
        qa = image.select('QA_PIXEL')
        mask = qa.bitwiseAnd(1<<8).gt(2).Or(qa.bitwiseAnd(1<<9)).gt(2)
        return image.updateMask(mask)

How can I use the 'bitwiseAnd' function in this context and mask the "high" cloud confidence parts?

I have tried to do adapt my code to this question: Using cloud confidence to create cloud mask from Landsat 8 BQA? but this results in getting this error:

Image.rightShift: Bitwise operands must be integer only.

1 Answer 1


I think I found the solution for this.

def bitwiseExtract(input, fromBit, toBit):
  maskSize = ee.Number(1).add(toBit).subtract(fromBit)
  mask = ee.Number(1).leftShift(maskSize).subtract(1)
  return input.rightShift(fromBit).bitwiseAnd(mask)

qa = image.select('QA_PIXEL')
cloudQualityMask = bitwiseExtract(qa, 8, 9)
cloud_mask = qa.bitwiseAnd(cloud_shadow_bit_mask).lt(3)
imageMasked = image.updateMask(cloud_mask)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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