Last active
August 29, 2015 14:20
-
-
Save sdht0/728a77d20466943c931d to your computer and use it in GitHub Desktop.
Bit Operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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