Skip to content

Instantly share code, notes, and snippets.

@sdht0
Last active August 29, 2015 14:20
Show Gist options
  • Save sdht0/728a77d20466943c931d to your computer and use it in GitHub Desktop.
Save sdht0/728a77d20466943c931d to your computer and use it in GitHub Desktop.
Bit Operations
// printbinary(100069) -> 0000 0000 0000 0001 1000 0110 1110 0101
void printbinary(unsigned int n)
{
int i;
int N=(int)sizeof(n)*8;
for(i=N-1;i>=0;i--)
{
printf("%d%s",(n>>i)&1,(i%4==0?" ":""));
}
printf("\n");
}
//From wikipedia: Wegner's algorithm
int noofbitsinnumber(unsigned val) {
int dist = 0;
while (val != 0)
{
// A bit is set, so increment the count and clear the bit
dist++;
val &= val - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment