0

I have come across a C++ function in my work that extracts integer numbers from a 16bit unsigned integer using bit-masking.

I need to port this over to Python, and am unsure if the same syntax is valid between the languages, or if there are any special differences between the implementations.

My C++ function is this:

 void BitMapper(unsigned int x){

    unsigned int a=0;
    unsigned int b=0;
    unsigned int c=0;

    unsigned int aMask=0xff00; 
    unsigned int bMask=0x00f0;
    unsigned int cMask=0x000f;

    a=(x & aMask)>>8;
    b=(x & bMask)>>4;
    c=(x & cMask);

    std::cout<< a << "\t" << "b" << "\t"<< c <<std::endl;
 }

I have very naively implemented it in Python as this:

def BitMapper(x, aMask=0xff00, bMask=0x00f0, cMask=0x000f):
    
    ###Missing some method to cast x as unsigned 16 bit unsigned integer

    a = (x & aMask)>>8
    b = (x & bMask)>>4
    c = (x & cMask)
    
    print("\t {0} \t {1} \t {2}".format(a, b, c))
    

Is this implementation correct? If not, what have I done wrong and why?

I would be clear that I have never come across bit-masking before, so if there are stupid mistakes, it is because I am already puzzled about what is going on here anyway.

I know my input is of the format:

    a       b      c
00000000 | 0000 | 0000

and I assume that the << is doing a bit shift, presumably of the length of a and b by the values of the shift in the function definitions. As for the hexadecimal code of the masks, I have no idea what this means.

If someone could educate me on what exactly is going on in this function as well, I would be most appreciative.

Jack Rolph
  • 587
  • 5
  • 15
  • 2
    Why do you think this is wrong? Do you get different results? Yes, `>>` does a right shift. So `x >> 8` returns x shifted right by 8 (or, divided by 256). – Tim Roberts Mar 23 '22 at 18:04
  • I ask because I am completely ignorant of what I am doing - and I want to be sure I am not making a mistake because I am ignorant of the differences between Python and C++. For instance, I am unsure how to get Python to cast the input as a 16-bit integer. – Jack Rolph Mar 23 '22 at 18:10
  • 1
    Python does not have typed integers. All integers are signed values of infinite size. Your code does what the C++ code does. – Tim Roberts Mar 23 '22 at 18:13
  • Best way to ensure proper behaviour is test-driven development. Also, https://docs.python.org and https://cppreference.com should both be in your bookmarks. – Ulrich Eckhardt Mar 24 '22 at 09:50

1 Answers1

0

Is this implementation correct?

Probably.

You should anyway start by testing it. If you always get the same result from the C++ and Python implementations (for a sane range of inputs, where everything fits in unsigned int), that should give you some confidence.

I would be clear that I have never come across bit-masking before

So write a load of toy tests until you understand what's happening. What is 1 & 1? What is 1 & 2? Do you understand why?

Python even has an interpreter, there's no cost to just writing a bunch of bitwise-and expressions in there and seeing if the result is what you expected.

... I assume that the << is doing a bit shift

You can read the Python documentation for this operation here and the C++ documentation here. All this stuff is documented and searchable (once you know it exists to be searched, and know roughly what to look for).

There's also a very detailed explanation on this site which I just found by typing "bitshift" into my search bar.

Again, you can (and should) confirm your understanding by just testing what values you get for eg. 1 << 1, 1 << 2, and perhaps more relevantly 0xff00 >> 8, and so on.

// These were originally without the type

The only concern is here: C++ doesn't allow un-typed declarations, so it's possible these a, b, c are really global or namespace scope variables. If that's the case then the original function has a side-effect you're not reproducing. Find out where a etc. are declared and see if they're used elsewhere to be sure.

Useless
  • 64,155
  • 6
  • 88
  • 132