简体   繁体   English

在C程序中解析32位整数

[英]Parsing 32 bit integer in C program

I have a 32 bit integer, split into parts like this: 我有一个32位整数,分成如下部分:

 --------------------------------------
 | Part1         | Part2    | Part 3  |
 --------------------------------------

Part 1 higher 16 bits. 第1部分高16位。 (Part 2 + Part 3) = lower 16 bits. (第2部分+第3部分)=低16位。

Part 2 is 10 bits and Part 3 is 6 bits 第2部分是10位,第3部分是6位

I need help on how do we read and update part 1, part2 and part 3 in C program. 我需要有关如何阅读和更新C程序的第1部分,第2部分和第3部分的帮助。

Given an integer x with the above format, you can replace Part2 like this: 给定具有上述格式的整数x ,您可以像这样替换Part2:

x = (x & ~(0x3ff << 6)) | (newPart2 << 6);

and Part3 like so: 和Part3像这样:

x = (x & ~0x3f) | newPart3;

This assumes that both newPart2 and newPart3 are eg unsigned int with their new values right-adjusted. 假设newPart2newPart3均为unsigned int且其新值已正确调整。

int i

To extract the individual parts 提取各个部分

part1 = (i & 0xFFFF0000) >> 16

part2 = (i & 0x0000FFC0) >> 6

part3 = (i & 0x0000003F) 

To compose the integer 组成整数

i = (part1 << 16) | (part2 << 6) | (part3)

Try cast to this structure 尝试强制转换为此结构

struct {
    uint32_t part_1:16;
    uint32_t part_2:10;
    uint32_t part_3:6;
} parts;

Could be the one below depending on endianness 可能是以下一种,具体取决于字节序

  struct {
        uint32_t part_1:6;
        uint32_t part_2:10;
        uint32_t part_3:16;
    } parts;

Obviously not portable! 显然不便携!

Since you need to read and update, a pointer will do. 由于您需要阅读和更新,因此可以使用指针。 For example, if you 32bit value is called x, you do the following 例如,如果您的32位值称为x,则执行以下操作

parts *ptr = (parts *)&x;

ptr->part_2 = <part2 update>

The theory to be used behind this are and, or and shift operations with masks. 在此后面要使用的理论是和,或与掩码的移位操作。

To access some bits of the integer, first create a mask where there are ones in the bits you want to be used. 要访问整数的某些位,请首先创建一个掩码,在其中要使用的位中有一个。 Now apply and and ( & ) operation between the mask and the integer. 现在在掩码和整数之间应用and and& )操作。 According to the behavior of the & the bits where the mask is 0 will be 0 and where the mask is 1 will have the value of that bit in the integer. 根据&的行为,掩码为0的位将为0,掩码为1的位将具有整数形式的该位的值。 Now that we have only the bits we want we align them to the right, that is done shifting the bits to the right the correct number of positions as to leave the rightmost bit of the mask in the less significant position of the byte. 现在我们只有我们想要的位,我们将它们向右对齐,这是通过将位向右移正确的位置数来完成的,以便将掩码的最右边的位保留在字节的较低有效位置。

To write in a part of a byte, we need fist to nullify what was in that part for that we use the negated mask that is used to read that part. 要写入字节的一部分,我们需要用拳头使该部分中的内容无效,因为我们使用了用于读取该部分的否定掩码。 Once that part is negated we apply an or ( | ) operation with the new value that must be aligned to that position. 取反该部分后,我们将对必须与该位置对齐的新值应用“ or| )操作。

To read: 读书:

unsigned int read_part_1(unsigned int composed) {
   return (composed & 0xffff0000) >> 16;
}

unsigned int read_part_2(unsigned int composed) {
   return (composed & 0x0000ffc0) >> 6;
}

unsigned int read_part_3(unsigned int composed) {
   return (composed & 0x0000003f);
}

To write(val aligned to the right): 要写(val右对齐):

unsigned int write_part_1(unsigned int composed, unsigned int val) {
   return (composed & ~0xffff0000) | ((val & 0x0000ffff) << 16);
}

unsigned int write_part_2(unsigned int composed, unsigned int val) {
   return (composed & ~0x0000ffc0) | ((val & 0x000003ff) << 10);
}

unsigned int write_part_3(unsigned int composed, unsigned int val) {
   return (composed & ~0x0000003f) | (val & 0x0000003f);
}

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

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