解释下面的C ++方法 [英] Explain the following C++ method
问题描述
#define XL 33
#define OR 113
#define NOR 313
#define TN 344
int to_bits(int critn,char *mask)
{
unsigned int x;
int begin;
if (critn < XL) begin = 1;
else if (critn < OR) begin = XL;
else if (critn < NOR) begin = OR;
else if (critn <= TN) begin = NOR;
else begin = 0;
x = critn - begin;
*mask = (char)(0x80 >> (x % 8));
return (int)(x >> 3); // fast divide by 8
}
我不知道C ++代码。
I don't have any knowledge of C++ code. Can any one explain what this method is doing in the last 2 lines?
感谢
推荐答案
在C ++中,就像大多数编程语言一样,您只能返回一个值。要返回两个值,这是一个常见的C / C ++实践,返回一个并传递一个指针到一个对象,并通过指针修改该对象( mask
在这种情况下)
In C++, just like most programming languages, you can only return one value. To "return" two values, it's a common C/C++ practice to return one and pass a pointer to an object and modify that object via the pointer (mask
in this case).
mask
指向的对象将被分配一个只有一个位设置的位掩码。这是通过获取十六进制值0x80(二进制形式为1000 0000),然后右移0到7步。确切的步数由 x
决定,它是使用某些特定应用程序逻辑的计算机。
The object that mask
point to will be assigned a bitmask with exactly one bit set. This is done be taking the hexadecimal value 0x80 (1000 0000 in binary form) and right shift it 0 to 7 steps. The exact number of steps is decided by x
, which is computer using some application-specific logic.
返回的是 x / 8
。
你可以看到例程作为一个除法例程返回 x / 8
和余数(如 x modulo 8 ,但表示为位掩码而不是整数值)。
You can see the routine as a division routine that returns x/8
and the remainder (like x modulo 8, but expressed as a bit mask rather than an integer value).
这篇关于解释下面的C ++方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!