【发布时间】:2018-03-04 00:20:24
【问题描述】:
我一直试图了解位操作如何与 C 和指针一起工作,但没有运气。这是我的问题。我在内存中有 8 个字节,需要提取前 61 位并打印。例如: 0000 0000..... 0010 0111 ,我需要提取值 32 (忽略 64 位的最后 3 位。)这在 C 中如何工作?我所拥有的只是一个指向 8 字节开头的指针。
uint_64_t* myPointer; // already pointing to start of 8bytes
typedef struct Solve{
uint64_t myBytes: 64; // set a field thats equal to 64 bits
}
struct Solve s1;
s1.myBytes = *myPointer>>3; //failed attempt
printf("Bytes are %" PRIu64 "\n", s1.myBytes); // just prints out 0 when should be 32
【问题讨论】:
-
你没有展示你如何设置
*myPointer的值,uint64_t myBytes: 64;是一个语法错误。您还混合了有符号和无符号类型。请发布显示问题的Minimal, Complete, and Verifiable example。 -
0000 0000..... 0010 0111或其等效编码在哪里? -
如果您希望从您发布的二进制文件中得到 32,则需要屏蔽低 3 位。
val &= ~(1<<3-1);
标签: c bit-manipulation bit bitmask