fork download
  1. #include<stdio.h>
  2. int main()
  3. {
  4. unsigned int v=1665; // count the number of bits set in v
  5. unsigned int c; // c accumulates the total bits set in v
  6.  
  7. // option 1, for at most 14-bit values in v:
  8. c = (v * 0x200040008001ULL & 0x111111111111111ULL) % 0xf;
  9.  
  10. // option 2, for at most 24-bit values in v:
  11. c = ((v & 0xfff) * 0x1001001001001ULL & 0x84210842108421ULL) % 0x1f;
  12. c += (((v & 0xfff000) >> 12) * 0x1001001001001ULL & 0x84210842108421ULL)%0x1f;
  13.  
  14. // option 3, for at most 32-bit values in v:
  15. c = ((v & 0xfff) * 0x1001001001001ULL & 0x84210842108421ULL) % 0x1f;
  16. c += (((v & 0xfff000) >> 12) * 0x1001001001001ULL & 0x84210842108421ULL) % 0x1f;
  17. c += ((v >> 24) * 0x1001001001001ULL & 0x84210842108421ULL) % 0x1f;
  18. printf("%d",c);
  19. return 0;
  20. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
4