Masking bits

More medical imaging processing headaches…

I have a Xojo.Core.MemoryBlock that was created by reading raw bits from a file. These raw bits represent pixel data that I am trying to convert into a Xojo Picture.

Depending on the type of image (CT, plain film, MRI, etc) each pixel is either 8, 16 or 24 bits. In some cases, not all of those bits are utilised. For example, it seems common for each pixel to be 16 bits (so I could get this value by reading a UInt16 from the MemoryBlock). However, sometimes, of the 16 bits allocated, only 12 bits are used (as a lot of greyscale information in the pixel only has 4096 shades of grey).

What I don’t know how to do is mask the unused bits to get the actual value of the pixel.

Here’s another explanation (from

How would I read a UInt16 from the MemoryBlock but mask out the remaining 4 bits?

If you only want to keep the top 12 bits of a UInt16, you could do this:

Dim value As UInt16 = myValue And &b1111111111110000 // or the hex &hFFF0