简体   繁体   中英

Bit-masking - C++ to Python

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM