You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LearnCCpp/practice/g_bitFlagForStruct.c

32 lines
811 B

#include <stdio.h>
typedef struct
{
unsigned int data : 10; // 10비트, 0~1023
unsigned int error_code : 6; // 6비트 플래그
} DataPacket;
int main()
{
DataPacket packet;
packet.data = 108;
packet.error_code = 0;
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
// 에러 감지
// 0번째 비트 켜기
packet.error_code |= 1;
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
// 3번째 비트 켜기
packet.error_code |= (1 << 3);
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
// 5번째 비트 켜기
packet.error_code |= (1 << 5);
printf("[Packet] data: %d, error_code: 0x%x\n", packet.data, packet.error_code);
return 0;
}