解析一个位域参数,如何“丢弃”无符号长整型中的位?
首先,我想知道这是否可能:假设我有一个无符号长整型,其中包含一些任意无符号短整型,它们可能在也可能不在数字中。例如:
unsigned short int id1 = 3456,
id2 = 30998;
unsigned long long bitfld = id1|id2;
其他2个字段可以假设为0吗? OR 是正确的操作吗?之后,假设我将 bitfld 作为参数传递:
void dostuff (unsigned long long bf)
{
//pseudo code
if( the first field exists in bf)
get first field;
if( the second field exists in bf)
get second field;
//etc...
}
我想我必须轮询位域的前 16 位并检查它们,然后递归地轮询它们,验证它们并存储它们(如果它们大于 0)。不知道如何做到这一点,位移位仅向左或向右移动,因此,它仅向右除或乘?
抱歉撞到了。感谢大家的回答,但我最终使用了一种更简单、更有效的方法,即内部结构。您看,我可以使用字符串轻松完成此操作,但我的目的是对代码用户透明,可以说易于编程。 我创建了一个内部结构来保存我的值,然后创建一个公共方法来创建和返回这样的结构,因此它很容易使用并且解析速度更快(尽管它有在堆栈中分配一个(尽管很小)结构的开销,这位域解决方案还没有,但是唉)。
谢谢大家的回答。
First of all, I want to know if this is possible: let's say I have an unsigned long which contains some abritrary unsigned shorts, which may or may not be in the number. For example:
unsigned short int id1 = 3456,
id2 = 30998;
unsigned long long bitfld = id1|id2;
Can the other 2 fields be assumed as 0? And is OR the right operation for this? After that let's say I pass bitfld as an argument:
void dostuff (unsigned long long bf)
{
//pseudo code
if( the first field exists in bf)
get first field;
if( the second field exists in bf)
get second field;
//etc...
}
I think I have to poll out the first 16 bits of the bitfield and check those, then recursively poll them, verify them and store them if they are greater than 0. But I'm not sure how to do this, bit shifting only shifts left or right, thus, it only divides or multiplies right?
sorry for the bump. Thanks all for your answers, but I ended up using a simpler and more efficient method, an internal structure. You see, I could have done this easily with a string, but my purpose was transparency to the user of the code, easy to program so to say.
I created an internal structure to hold my values and then a public method to create and return such structure, so it is easy to use and faster to parse (though it has the overhead of allocating in the stack a (albeit small) structure, which the bit field solution hasn't, but alas).
So thank you all for your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
short int
的长度是 2 个字节,但long long
的长度是 8 个字节,因此存在某种长度不匹配的情况;您的意思可能是这样的:
您可以检查是否有一个字段被
AND
占用,如下所示:short int
is 2 bytes long, butlong long
is 8 bytes, so you have some kind of length mismatch;You may have meant this:
you can check is there is a field occupied by
AND
ing it like:按位或运算不是您想要的。该操作会将现有位与新位合并。您需要一个替换位的操作。
因此,首先,您需要使用 AND 和 NOT 清除这些位:
除非内存空间不足是一个问题,否则这种位打包不值得开发工作、验证时间和额外的执行时间。将这些值放入无符号字符的“打包”缓冲区中,然后将该缓冲区用于 I/O。
The bitwise OR operation is not what you want. The operation will merge existing bits with the new bits. You want an operation that replaces bits.
So first, you will need to clear out the bits using AND and NOT:
Unless lack of memory space is an issue, this kind of bit packing is not worth the development effort, validation time and extra execution time. Place the values into a "packed" buffer of unsigned char, then use the buffer with I/O.
您可能应该回顾一下移位运算符及其工作原理。如果你只是这样做:
,您基本上将所有位混合在一起。您稍后将无法单独提取这些值。您想要做的事情类似于id1 | id2
id1 | (id2 << 16)
正如用户 alemjerus 所指出的。另一种无需麻烦即可实现相同目标的方法是使用联合:
现在您可以执行以下操作:
在 dostuff 中,您可以通过以下方式轻松检索字段:
You probably should review the bitshift operators and how they work. If you simply do:
, you basically mashed all the bits together. You would not be able to extract these values individually later. What you wanted to do is something likeid1 | id2
id1 | (id2 << 16)
as pointed out by the user alemjerus.Another way of accomplishing the same goal without bitshitting is to use a union:
Now you can do:
And in dostuff, you can easily retrieve the fields by:
假设 unsigned Short 为 2 个字节,unsigned long 为 4 个字节。
这有帮助吗?
处理位时,如果使用十六进制值,则更容易直观地看到发生的情况。
Assuming unsigned short is 2 bytes and an unsigned long is 4 bytes.
Does this help?
When dealing with bits it's easier to visually see what is happening if you work with hex values.