-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDay6.BitwiseOperators.js
69 lines (47 loc) · 1.79 KB
/
Day6.BitwiseOperators.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Objective
// Today, we're practicing bitwise operations. Check the attached tutorial for more details.
// Task
// We define to be a sequence of distinct sequential integers from to ; in other words, . We want to know the maximum bitwise AND value of any two integers, and (where ), in sequence that is also less than a given integer, .
// Complete the function in the editor so that given and , it returns the maximum .
// Note: The symbol represents the bitwise AND operator.
// Input Format
// The first line contains an integer, , denoting the number of function calls.
// Each of the subsequent lines defines a dataset for a function call in the form of two space-separated integers describing the respective values of and .
// Constraints
// Output Format
// Return the maximum possible value of for any in sequence .
// Sample Input 0
// 3
// 5 2
// 8 5
// 2 2
// Sample Output 0
// 1
// 4
// 0
// Explanation 0
// We perform the following function calls:
// When and , we have the following possible and values in set :
// The maximum of any that is also is , so we return .
// When and , the maximum of any in set is (see table above), so we return .
// When and , the maximum of any in set is (see table above), so we return .
// Sample Input 1
// 2
// 9 2
// 8 3
// Sample Output 1
// 1
// 2
// Explanation 1
// We perform the following function calls:
// When and , the maximum of any in set is (see table above), so we return .
// When and , the maximum of any in set is (see table above), so we return .
/*
* Return the largest value of any a & b < k in S (where a < b).
*
* n: Set S is a sequence of distinct integers from 1 to n (i.e., {1, 2, ..., n}).
* k: An integer.
*/
function getMaxLessThanK(n, k) {
return ((k | (k - 1)) > n) ? (k - 2) : (k - 1);
}