Bitwise Operators

by trentHarlem

HTML

<p>
5 2<br>
8 5<br>
2 2<br>
</p>
<p>
https://www.w3schools.com/js/js_bitwise.asp
</p>

JavaScript

//When a bitwise AND is performed on a pair of bits, it returns 1 if both bits are 1.

function getMaxLessThanK(n, k) {
    return ((k | (k - 1)) > n) ? (k - 2) : (k - 1);
}
console.log(getMaxLessThanK(5,2))
console.log(getMaxLessThanK(8,5))
console.log(getMaxLessThanK(2,2))