Day29 Bitwise AND

Day29 Bitwise AND

Objective

Welcome to the last day! Today, we’re discussing bitwise operations. Check out the Tutorial tab for learning materials and an instructional video!


Task

Given set . Find two integers, A and B (where A < B ), from set S such that the value of A & B is the maximum possible and also less than a given integer, K.
In this case, & represents the bitwise AND operator.


Input Format

The first line contains an integer, T, the number of test cases.
Each of the T subsequent lines defines a test case as 2 space-separated integers, N and K, respectively.


Constraints


Output Format

For each test case, print the maximum possible value of on a new line.


Sample Input

1
2
3
4
3
5 2
8 5
2 2

Sample Output

1
2
3
1
4
0

Explanation

All possible values of and are:

The maximum possible value of A&B that is also < (K = 2) is 1, so we print 1 on a new line.




Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function findMax(n, k) {
var max = 0;
for (let i = 0; i <= n; i++) {
for (let j = 0; j < i; j++) {
let value = j & i;
if (value < k && value > max) {
max = value;
}
}
}

return max;
}

function main() {
const t = parseInt(readLine(), 10);

for (let tItr = 0; tItr < t; tItr++) {
const nk = readLine().split(' ');

const n = parseInt(nk[0], 10);

const k = parseInt(nk[1], 10);

console.log(findMax(n, k));
}
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×