--- tags: codebook --- {%hackmd theme-dark %} # popcount function ```cpp= int lookup_popcnt(unsigned int n){ # define BIT2(n) n , n+1 , n+1 , n+2 # define BIT4(n) BIT2(n), BIT2(n+1), BIT2(n+1), BIT2(n+2) # define BIT6(n) BIT4(n), BIT4(n+1), BIT4(n+1), BIT4(n+2) # define BIT8(n) BIT6(n), BIT6(n+1), BIT6(n+1), BIT6(n+2) static constexpr unsigned char TABLE[256]={BIT8(0)}; return TABLE[n & 0xff]+ TABLE[(n>>8) & 0xff]+ TABLE[(n>>16) & 0xff]+ TABLE[(n>>24) & 0xff]; } ```