null
vuild_
Nodes
Flows
Hubs
Login
MENU
Notifications
Login
⌂
c-lang-advanced
Structure
advanced-pointers
•
Function Pointer
•
Pointer to Pointer
•
Void Pointer
data-structures
•
Linked List
•
Stack & Queue
•
Tree & Graph
algorithms
•
Sorting Algorithms
•
Complexity Analysis
bit-ops
•
Bitwise Operators
•
Bit Fields
•
Bit Tricks
system-prog
•
Process
•
Signal
•
IPC (Inter-Process Communication)
concurrency
•
Threads Basics
•
Mutex & Semaphore
•
Race Condition
optimization
•
Compiler Flags
•
Cache Locality
•
Profiling
design-patterns
•
OOP in C
•
Callback Pattern
•
State Machine
project
•
Mini Shell
•
TCP Echo Server
Flow Structure
Bit Fields
11 / 25
Process
☆ Star
↗ Full
Bit Tricks
#c
#c-lang
#advanced
#bit-ops
#bit-tricks
@devpc
|
2026-03-29 13:49:33
|
GET /api/v1/flows/6/nodes/75?fv=1&nv=1
Context:
Flow v1
→
Node v1
0
Views
0
Calls
# Bit Tricks > 2의 보수, 플래그 관리, 빠른 곱셈/나눗셈 트릭 ## 학습 목표 - 2의 보수 표현 방식을 이해한다 - 비트 연산을 이용한 실용적인 트릭을 익힌다 ## 내용 ### 2의 보수 ```c // 양수 5: 00000101 // 1의 보수: 11111010 // 2의 보수: 11111011 (== -5) int x = 5; int neg_x = ~x + 1; // == -5 ``` ### 빠른 곱셈/나눗셈 (2의 거듭제곱) ```c int x = 8; x << 1; // x * 2 = 16 x << 2; // x * 4 = 32 x >> 1; // x / 2 = 4 x >> 3; // x / 8 = 1 ``` ### 플래그 관리 패턴 ```c #define FLAG_READ (1 << 0) // 0001 #define FLAG_WRITE (1 << 1) // 0010 #define FLAG_EXEC (1 << 2) // 0100 int perms = 0; perms |= FLAG_READ | FLAG_WRITE; // 읽기 + 쓰기 설정 perms &= ~FLAG_WRITE; // 쓰기 해제 if (perms & FLAG_READ) { /* 읽기 가능 */ } ``` ### 유용한 비트 트릭 ```c // 짝수/홀수 판별 if (x & 1) { /* 홀수 */ } // 절댓값 (부호 있는 정수) int mask = x >> 31; int abs_x = (x + mask) ^ mask; // 두 수 swap (XOR) a ^= b; b ^= a; a ^= b; // 2의 거듭제곱 여부 확인 int is_power_of_2 = x && !(x & (x - 1)); ``` ## 참고 - 비트 트릭은 성능 최적화에 유용하지만 가독성이 낮아지므로 주석을 꼭 달 것
Bit Fields
Process
// COMMENTS
ON THIS PAGE
No content selected.