简体   繁体   English

位掩码 - C++ 到 Python

[英]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.我在我的工作中遇到了一个 C++ function 使用位掩码从 16 位无符号 integer 中提取 integer 数字。

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.我需要将其移植到 Python,并且不确定这些语言之间是否具有相同的语法,或者这些实现之间是否存在任何特殊差异。

My C++ function is this:我的 C++ function 是这样的:

 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:我在 Python 中非常天真地实现了它:

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.我假设<<正在做位移,大概是 a 和 b 的长度由 function 定义中的位移值。 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.如果有人能告诉我这个 function 到底发生了什么,我将不胜感激。

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.如果您总是从 C++ 和 Python 实现中得到相同的结果(对于合理的输入范围,其中所有内容都适合unsigned int ),那应该会给您一些信心。

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 ?什么是1 & 1 What is 1 & 2 ?什么是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. Python 甚至有一个解释器,只需在其中编写一堆按位和表达式并查看结果是否符合您的预期是没有成本的。

... I assume that the << is doing a bit shift ...我假设<<正在做一些转变

You can read the Python documentation for this operation here and the C++ documentation here .您可以在此处阅读此操作的 Python 文档和此处阅读 C++ 文档。 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.这个网站上还有一个非常详细的解释,我只是通过在搜索栏中输入“bitshift”找到的。

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. 1 << 11 << 2 ,也许更相关的是0xff00 >> 8 ,等等。

// 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.唯一的问题是:C++ 不允许非类型化声明,因此这些abc可能是真正的全局变量或命名空间 scope 变量。 If that's the case then the original function has a side-effect you're not reproducing.如果是这种情况,那么原始 function 会产生您无法复制的副作用。 Find out where a etc. are declared and see if they're used elsewhere to be sure.找出声明a等的位置,并确定它们是否在其他地方使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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