Tag: bit

在C中读取和写入64位64位

我有这个超级简单的代码,我读取8个字节的块(我将在代码中稍后加密它们),然后将它们写在一个新文件中。 它运行良好,但最后8个字节没有写入。 知道为什么吗? #include #include #include #include int main() { uint64_t data; FILE *input, *output; // create output file output = fopen(“output.txt”, “w”); // read file input = fopen(“test.txt”, “rb”); if(input) { while(fread(&data, 8, 1, input) == 1) { fwrite(&data, 8, 1, output); } size_t amount; while((amount = fread(&data, 1, 8, input)) > 0) { […]

在C中更改int中的位?

所以我有一个16位数。 假设它的变量名是Bits。 我想让Bits [2:0] = 001,100和000,而不改变其他任何东西。 我不知道该怎么做,因为我能想到的是ORing我希望与1成为1的位,但我不确定如何清除其他位以使它们为0.如果有人有建议,我很感激。 谢谢!

位字段及其对齐如何在C编程中起作用?

我需要你帮助理解位域在C编程中的工作原理。 我已经声明了这个结构: struct message { unsigned char first_char : 6; unsigned char second_char : 6; unsigned char third_char : 6; unsigned char fourth_char : 6; unsigned char fifth_char : 6; unsigned char sixth_char : 6; unsigned char seventh_char : 6; unsigned char eigth_char : 6; }__packed message; 我使用sizeof(message)将结构的大小保存为整数。 我认为大小的值将是6,因为6 * 8 = 48位,这是6个字节,但它的大小值为8个字节。 任何人都可以向我解释为什么,以及比特字段和它们的比对是如何工作的? 编辑 […]