#include <stdio.h> #include <stdlib.h> #include <string.h> /* 有符号 结构体1 */ struct bits { int b1:5; /* 因为是int型,最大设置32位,由于int型是有符号整形, 所以这里5位为有符号类型。 11111 -1 10000 -16 01111 15 00000 0 */ int :2; int b2:2; /* 11 -1 10 -2 01 1 00 0 */ int b3:1; /* 1 -1 0 0 */ }; /* 无符号 结构体2 */ struct bits { unsigned int b1:5; unsigned int :2; unsigned int b2:2; unsigned int b3:1; }; int main() { struct bits b; // int a = 0b1111111111111111; int a = 0b0100110101000101; memcpy(&b, &a, sizeof(b)); // memcpy(&b, "EM", sizeof(b)); printf("0x%X\n", b); printf("b.b1 = %d\n", b.b1); printf("b.b2 = %d\n", b.b2); printf("b.b3 = %d\n", b.b3); } /* int a = 0b0100110101000101; 0x4D45 b.b1 = 5 b.b2 = -2 b.b3 = 0 int a = 0b1111111111111111; 0xFFFF b.b1 = -1 b.b2 = -1 b.b3 = -1 int a = 0b0100110101000101; 0x4D45 b.b1 = 5 b.b2 = 2 b.b3 = 0 int a = 0b1111111111111111; 0xFFFF b.b1 = 31 b.b2 = 3 b.b3 = 1 */
结构体位制 中存在 有符号 与 无符号 -- C
时间: 2024-10-12 22:50:30