Skip to content

Commit e29e0fa

Browse files
Biswise AND number of range
Signed-off-by: Leo Ma <[email protected]>
1 parent 048ee99 commit e29e0fa

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test and.c
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int rangeBitwiseAnd(int m, int n)
5+
{
6+
int i, res = 0;
7+
for (i = 0; m > 0 && n > 0; i++) {
8+
if (m == n && (m & 1)) {
9+
res |= 1 << i;
10+
}
11+
12+
m = m >> 1;
13+
n = n >> 1;
14+
}
15+
16+
return res;
17+
}
18+
19+
int main(int argc, char **argv)
20+
{
21+
if (argc != 3) {
22+
fprintf(stderr, "Usage: ./test m n\n");
23+
exit(-1);
24+
}
25+
26+
printf("%d\n", rangeBitwiseAnd(atoi(argv[1]), atoi(argv[2])));
27+
return 0;
28+
}

0 commit comments

Comments
 (0)