We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 048ee99 commit e29e0faCopy full SHA for e29e0fa
201_bitwise_and_of_numbers_range/Makefile
@@ -0,0 +1,2 @@
1
+all:
2
+ gcc -O2 -o test and.c
201_bitwise_and_of_numbers_range/and.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#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