Skip to content

Commit 320e6d9

Browse files
authored
Add files via upload
1 parent ff314a2 commit 320e6d9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: Bit-Manipulation/EvenorOdd.js

+23
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)