Skip to content

Instantly share code, notes, and snippets.

@pbassut
Created May 12, 2017 01:16
Show Gist options
  • Save pbassut/9a377d82a4399ebe4c8e20eeefbe02a8 to your computer and use it in GitHub Desktop.
Save pbassut/9a377d82a4399ebe4c8e20eeefbe02a8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
uint32_t NumberOfSetBits(uint32_t i)
{
return (((((i - ((i >> 1) & 0x55555555)) & 0x33333333) + (((i - ((i >> 1) & 0x55555555)) >> 2) & 0x33333333) + (((i - ((i >> 1) & 0x55555555)) & 0x33333333) + (((i - ((i >> 1) & 0x55555555)) >> 2) & 0x33333333) >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
int main(){
printf("%d", NumberOfSetBits(127));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment