fork download
  1. #include <stdio.h>
  2.  
  3. int bit_count(int x)
  4. {
  5. int retVal;
  6.  
  7. int fives;
  8. fives = 0x55;
  9. fives |= fives << 0x08;
  10. fives |= fives << 0x10;
  11.  
  12. int threes;
  13. threes = 0x33;
  14. threes |= threes << 0x08;
  15. threes |= threes << 0x10;
  16.  
  17. int ofs;
  18. ofs = 0x0f;
  19. ofs |= ofs << 0x08;
  20. ofs |= ofs << 0x10;
  21.  
  22. int ooffs;
  23. ooffs = 0xff;
  24. ooffs |= ooffs << 0x10;
  25.  
  26. int ffff;
  27. ffff = 0xff;
  28. ffff |= ffff << 0x08;
  29.  
  30. retVal = x;
  31. retVal = ( retVal & fives ) + ( retVal >> 0x01 & fives );
  32. retVal = ( retVal & threes ) + ( retVal >> 0x02 & threes );
  33. retVal = ( retVal & ofs ) + ( retVal >> 0x04 & ofs );
  34. retVal = ( retVal & ooffs ) + ( retVal >> 0x08 & ooffs );
  35. retVal = ( retVal & ffff ) + ( retVal >> 0x10 & ffff );
  36.  
  37. return retVal;
  38. }
  39.  
  40. int main(void)
  41. {
  42. printf("%d\n", bit_count(0b11110000111100001111000011110000));
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 4392KB
stdin
Standard input is empty
stdout
16