We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ff314a2 commit 320e6d9Copy full SHA for 320e6d9
Bit-Manipulation/EvenorOdd.js
@@ -0,0 +1,23 @@
1
+/**
2
+ *
3
+ * This script will check whether a number is even or odd
4
+ * using bit manipulation.
5
+ * Idea:
6
+ * A number can be determined as even or odd by the lsb(least significant bit) or the
7
+ * right most bit in binary representation
8
+ * if we simply perform an and operation with 1 and the number we can determine:
9
+ * eg:
10
+ * number & 1 == 1, the number is odd
11
+ * number & 1 == 0, the number is even
12
13
+ * More about it :
14
+ * https://www.geeksforgeeks.org/check-if-a-number-is-odd-or-even-using-bitwise-operators/
15
16
+ */
17
+
18
+export const evenOrOdd = (number) => {
19
+ if (typeof number !== 'number' || !Number.isInteger(number)) {
20
+ throw new Error('Input must be an integer.')
21
+ }
22
+ return number & 1 ? 'odd' : 'even'
23
+}
0 commit comments