We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 29: Bitwise AND
Day 29: Bitwise AND
Sort by
recency
|
431 Discussions
|
Please Login in order to post a comment
def bitwiseAnd(N, K): max_and = 0 for b in range(2, N + 1): for a in range(1, b): current_and = a & b if max_and < current_and < K: max_and = current_and # Optimization: if we hit K-1, we found the highest possible value if max_and == K - 1: return max_and return max_and
in python -
def bitwiseAnd(N, K): if K - 1 | K <= N: return K -1 else: return K-2
any one explain why this code works always int bitwiseAnd(int n, int k) { if (((k - 1) | k) <= n) { return k - 1; } else { return k - 2; } }
PYTHON 3 SOLUTION
python: