aboutsummaryrefslogtreecommitdiff
path: root/bitwise_and_num_range.c
diff options
context:
space:
mode:
Diffstat (limited to 'bitwise_and_num_range.c')
-rw-r--r--bitwise_and_num_range.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/bitwise_and_num_range.c b/bitwise_and_num_range.c
new file mode 100644
index 0000000..ce137c1
--- /dev/null
+++ b/bitwise_and_num_range.c
@@ -0,0 +1,23 @@
+// 201. Bitwise AND of Numbers Range
+#include "leetcode.h"
+
+/*
+ * given two integers 'm' and 'n' that represent the range '[m,
+ * n]', return the bitwise and of all numbers in this range, inclusive.
+ */
+
+int rangeBitwiseAnd(int m, int n) {
+ int k = 0;
+ while (m != n) {
+ m >>= 1;
+ n >>= 1;
+ ++k;
+ }
+ return n << k;
+}
+
+int main() {
+ printf("%d\n", rangeBitwiseAnd(5, 7)); // expect: 4
+ printf("%d\n", rangeBitwiseAnd(0, 0)); // expect: 0
+ printf("%d\n", rangeBitwiseAnd(1, 2147483647)); // expect: 0
+}