[英]How to use bit field to get whole row bits?
#include <stdio.h>
#include <stdint.h>
struct __attribute__((__packed__)) sf_header
{
uint64_t lower: 32;
uint64_t higher: 32;
};
int main()
{
struct sf_header h;
// part 1;
h.lower = 15;
h.higher = 1;
printf("lower part: %d\n", h.lower);
printf("higher part: %d\n", h.higher);
//part 2: how to printf the whole 64-bits (a row)
unsigned long int lower = h.lower;
unsigned long int higher = h.higher;
higher = higher << 32;
printf("lower: %ld\n", lower);
printf("higher: %ld\n", higher);
unsigned long int final = lower | higher;
printf("Final: %ld\n", final);
return 0;
}
It runs on 64-bits and the long int is 8 bytes which is a row sized.它在 64 位上运行,long int 是 8 个字节,这是一行大小。
First, I assign two values into the lower 32 bits and higher 32 bits.首先,我将两个值分配给低 32 位和高 32 位。
What if I want to get the 64 bits and use it?如果我想获得 64 位并使用它怎么办?
Do I need to do the part 2 or there is a easy way of bit field to do it?我需要做第 2 部分还是有一种简单的位域方法来做到这一点?
Weather Vane just fractionally ahead of me...!风向标就在我前面一点点......!
You use a union.你使用联合。 There's one in the Win32 headers (not sure I like it) called ULARGE_INTEGER but here's a quick union example which paraphases that union:
Win32 标头中有一个(不确定我是否喜欢它)称为 ULARGE_INTEGER 但这里有一个快速联合示例,它与该联合相辅相成:
typedef union {
struct {
int32_t LowPart;
int32_t HighPart;
} u;
uint64_t QuadPart;
} ULARGE_INTEGER;
Thus if you had ULARGE_INTEGER a, a.QuadPart would give you the 64 bit part and auLowPart would give you the low 32 bits.因此,如果您有 ULARGE_INTEGER a,a.QuadPart 将为您提供 64 位部分,而 auLowPart 将为您提供低 32 位。 Or high 32 bits if your machine was big-endian!
如果你的机器是大端的,或者高 32 位!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.