Maximize XOR Value
You are provided with an integer X
and are tasked with identifying an integer Y
such that the bitwise XOR operation between X
and Y
yields the maximum possible value. The condition is that Y
must not exceed 2305843009213693951
which is (2^61) - 1
.
Explanation:
The bitwise XOR operation contrasts two bit patterns of equivalent length, conducting an exclusive OR operation on every corresponding bit pair. The resultantly placed bit is 1
if precisely one of the paired bits is 1
; otherwise, it defaults to 0
if both bits are 0
or both are 1
.
Input:
The first input line includes an integer T
indicating the number of test cases.
Each test case input consists of a single integer X
.
Output:
For every test case, the output is an integer Y
which, when XORed with X
, results in the maximum achievable value, each result appearing on a new line.
Example:
Input:
2
3
7
Output:
2305843009213693950
2305843009213693944
Constraints:
1 ≤ T ≤ 104
0 ≤ X ≤ 1018
- Time Limit: 1 second
Note:
No need to print any values explicitly as the function's implementation manages the outputs.
Ensure that the maximum achievable value can be accommodated within a 64-bit memory space.
The initial integer X
is always a non-negative number.
Find an integer Y such that XOR operation with X yields maximum value within given constraints.
Iterate from the most significant bit to find the highest bit that can be toggled to maximize XOR value.
T...read more
Top Urban Company Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Urban Company Software Developer
Reviews
Interviews
Salaries
Users/Month