-
C스러운 코드로 유혹하기. (비트연산, 연산우선순위 편)Life: research & education/Research 2023. 12. 9. 13:00
오늘은 코드로 상대를 유혹하는 방법을 알아보겠습니다.
모든 코드는 ANSI문법을 준수합니다.
01. 분기없이 Max, min 구하기
#define MIN(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y)))) #define MAX(x, y) ((x) ^ (((x) ^ (y)) & -((x) < (y))))
02. N을 32로 나눈 나머지
N & 0x1F
03. log N값 구하기
const int tab64[64] = { 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5 }; #define PROCESS_KECC_BDGT1(x) x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x |= x >> 32; #define PROCESS_KECC_BDGT2(x) ((unsigned long long)((x - (x >> 1)) * 0x07EDD5E59A4E28C2)) >> 58 int BitSize(unsigned long long logVal) { PROCESS_KECC_BDGT1(logVal) value = tab64[PROCESS_KECC_BDGT2(logVal)]; return value; }
04. 매 500번마다 해당 코드가 반복되게. 단, 0은 제외해라
for (int idx = 0;idx < 10000;idx++){ // ... if (idx % 500 == !idx) { printf("중간상황을 보고합니다."); } // ... }
'Life: research & education > Research' 카테고리의 다른 글
ESP32 포트 인식이 안 될 때 의심할 수 있는 문제 (1) 2024.08.28 서울대학교 성적증명서 Microsoft print to pdf 지원하지 않는 포트 해결법 (2) 2024.07.19 Plane sweep algorithm (0) 2021.12.04 C언어 NIST a statistical Test Suite for Random and Pseudo random Number Generators for Ctyptographic Applications에 관련된 자료 (0) 2021.05.22 C언어 math.h에서 파이값 가져오는 방법 (0) 2021.05.15