0

I am trying to rewrite a small portion of C++ code in python. Below is a snippet with the unimportant parts removed. It simply assigns to the variable z the value of the depth_image at location [x,y]. However, I don't understand the purpose of the shift operation <<.

const ::CByteImage& depth_image
const unsigned int offset = (y * width + x) * 3;
const float z = static_cast<unsigned int>(
               depth_image.pixels[offset + /* R = */ 0]
            + (depth_image.pixels[offset + /* G = */ 1] << /*   sizeof(char) = */  8)
            + (depth_image.pixels[offset + /* B = */ 2] << /* 2*sizeof(char) = */ 16)
        );

If read in python, the variable depth_image is an array of shape [640,480,3]. Would the equivalent simply be to sum all 3 channels?

depth_image = cv2.imread(depth_filename)
x,y = 0,0
z = depth_image[y,x,0] + (depth_image[y,x,1] << 8) + (depth_image[y,x,2] << 16)

I have zero knowledge in C++.

Kong
  • 2,202
  • 8
  • 28
  • 56
  • Python has the same bitwise operators as C++. You can do `<< 8` in Python. See the linked question for an overview and examples. – John Kugelman May 15 '22 at 14:45
  • @JohnKugelman thanks! would the edited python code be equivalent to the C++ code then? – Kong May 15 '22 at 14:54

0 Answers0