Compare commits
3 Commits
7d7ac8ec82
...
fd6449c05c
Author | SHA1 | Date |
---|---|---|
|
fd6449c05c | 5 days ago |
|
3a1d21f8cf | 5 days ago |
|
ede38f800e | 5 days ago |
@ -0,0 +1,37 @@ |
|||||||
|
#include <stdio.h> |
||||||
|
|
||||||
|
int main() |
||||||
|
{ |
||||||
|
|
||||||
|
int a = 6; // 0110
|
||||||
|
int b = 3; // 0011
|
||||||
|
int result; |
||||||
|
|
||||||
|
printf("result size: %d\n", sizeof(int)); |
||||||
|
|
||||||
|
// AND(&)
|
||||||
|
result = a & b; // 0010 => 2
|
||||||
|
printf("[AND] %d : %x\n", result, result); |
||||||
|
|
||||||
|
// OR(|)
|
||||||
|
result = a | b; // 0111 => 7
|
||||||
|
printf("[OR] %d : %x\n", result, result); |
||||||
|
|
||||||
|
// XOR(^): 두 비트가 다르면 1 반환
|
||||||
|
result = a ^ b; // 0101 => 5
|
||||||
|
printf("[XOR] %d : %x\n", result, result); |
||||||
|
|
||||||
|
// NOT(~): 비트 반전
|
||||||
|
result = ~a; // 1111 1001 => -7
|
||||||
|
printf("[NOT] %d : %x\n", result, result); |
||||||
|
|
||||||
|
// Left Shift(<<): 비트 시프트
|
||||||
|
result = a << 1; // 1100 => 12
|
||||||
|
printf("[<<] %d : %x\n", result, result); |
||||||
|
|
||||||
|
// Right Shift(<<): 비트 시프트
|
||||||
|
result = a >> 1; // 0011 => 3
|
||||||
|
printf("[>>] %d : %x\n", result, result); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
#include <stdio.h> |
||||||
|
|
||||||
|
#define FLAG_A 0x01 // 0000 0001
|
||||||
|
#define FLAG_B 0x02 // 0000 0010
|
||||||
|
#define FLAG_C 0x04 // 0000 0100
|
||||||
|
|
||||||
|
int main() |
||||||
|
{ |
||||||
|
unsigned char flags = 0; |
||||||
|
|
||||||
|
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X')); |
||||||
|
|
||||||
|
// A 활성화
|
||||||
|
flags |= FLAG_A; |
||||||
|
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X')); |
||||||
|
|
||||||
|
// C 활성화
|
||||||
|
flags |= FLAG_C; |
||||||
|
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X')); |
||||||
|
|
||||||
|
// B 활성화
|
||||||
|
flags |= FLAG_B; |
||||||
|
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X')); |
||||||
|
|
||||||
|
// B 비활성화
|
||||||
|
flags &= ~FLAG_B; |
||||||
|
printf("A: %c, B: %c, C: %c\n", (flags & FLAG_A ? 'O' : 'X'), (flags & FLAG_B ? 'O' : 'X'), (flags & FLAG_C ? 'O' : 'X')); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,31 @@ |
|||||||
|
#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; |
||||||
|
} |
Loading…
Reference in new issue