File tree Expand file tree Collapse file tree 4 files changed +54
-0
lines changed Expand file tree Collapse file tree 4 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ all :
2+ gcc -O2 -o test reverse_bits.c
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <stdint.h>
4+
5+ static uint32_t reverseBits (uint32_t n )
6+ {
7+ int i ;
8+ uint32_t res = 0 ;
9+ for (i = 0 ; i < 32 ; i ++ ) {
10+ res <<= 1 ;
11+ res |= n & 0x1 ;
12+ n >>= 1 ;
13+ }
14+ return res ;
15+ }
16+
17+ int main (int argc , char * * argv )
18+ {
19+ if (argc != 2 ) {
20+ fprintf (stderr , "Usage: ./test n\n" );
21+ exit (-1 );
22+ }
23+
24+ printf ("%u\n" , reverseBits (atoi (argv [1 ])));
25+ return 0 ;
26+ }
Original file line number Diff line number Diff line change 1+ all :
2+ gcc -O2 -o test hamming_weight.c
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <stdint.h>
4+
5+ static int hammingWeight (uint32_t n )
6+ {
7+ int count = 0 ;
8+ while (n > 0 ) {
9+ n = n & (n - 1 );
10+ count ++ ;
11+ }
12+ return count ;
13+ }
14+
15+ int main (int argc , char * * argv )
16+ {
17+ if (argc != 2 ) {
18+ fprintf (stderr , "Usage: ./test n\n" );
19+ exit (-1 );
20+ }
21+
22+ printf ("%d\n" , hammingWeight (atoi (argv [1 ])));
23+ return 0 ;
24+ }
You can’t perform that action at this time.
0 commit comments