Skip to content

Commit eafa1b5

Browse files
Create minMaxElement.js
1 parent 0ca18c2 commit eafa1b5

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Bit-Manipulation/minMaxElement.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @author : nikgautamgithub
3+
*
4+
* Find max or min element using bit operators.
5+
* @param {number} x - The input number to compare.
6+
* @param {number} y - The input number to compare.
7+
* @returns Min/Max Element
8+
*
9+
* @example
10+
* const maxi = max(5,3) // Returns max number i.e. 5
11+
* const mini = min(5,3); // Returns min number i.e. 3
12+
*/
13+
const max = (x, y) => x ^ ((x ^ y) & -(x < y ? 1 : 0));
14+
15+
const min = (x, y) => y ^ ((x ^ y) & -(x < y ? 1 : 0));
16+
17+
export { min, max };

0 commit comments

Comments
 (0)