반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #ifndef __S_BIT_H__ #define __S_BIT_H__ typedef long long s_bit_t; #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif #define S_BIT_SET(NR, BITS) (NR |= (1LL << BITS)); #define S_BIT_SET_ALL(NR) (NR = -1LL); #define S_BIT_CLR(NR, BITS) (NR &= ~(1LL << BITS)); #define S_BIT_CLR_ALL(NR) (NR = ~(-1LL)); #define S_BIT_IS_SET(NR, BITS) ((NR & (1LL << BITS)) ? TRUE : FALSE) #define S_BIT_IS_CLR(NR, BITS) ((!(NR & (1LL << BITS))) ? TRUE : FALSE) #endif /* __S_BIT_H__*/ |
사용 예시
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> #include "s_bit.h" int main() { s_bit_t bits = 0; S_BIT_SET_ALL(bits); S_BIT_CLR_ALL(bits); S_BIT_SET(bits, 4); S_BIT_SET(bits, 8); printf("%d\n", S_BIT_IS_SET(bits, 7)); printf("%d\n", S_BIT_IS_SET(bits, 4)); printf("%d\n", S_BIT_IS_CLR(bits, 4)); printf("%d\n", S_BIT_IS_CLR(bits, 7)); return 0; } $./test 0 1 0 1 | cs |
반응형
'Language > C,C++' 카테고리의 다른 글
[C++] K-means clustering (0) | 2022.02.15 |
---|---|
표준출력에 텍스트 색상 입히기 (colorize printf format) (0) | 2018.10.09 |
[macro] 안전한 형변환을 위한 Macro (0) | 2018.08.09 |
[C] #pragma pack( [show] | [push | pop] , n ) (0) | 2018.07.06 |
[C++] atomic_flag (0) | 2018.06.26 |