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.
AnswerBot
4mo

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

Help your peers!
Select
Add answer anonymously...

Urban Company Software Developer interview questions & answers

A Software Developer was asked 9mo agoQ. What is a thread?
A Software Developer was asked 10mo agoQ. You are given an array of prices where prices[i] is the price of a given stock o...read more
A Software Developer was asked 10mo agoQ. Given a sorted array of integers nums and an integer target, write a function th...read more

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. What is a thread?
A Software Developer was asked 10mo agoQ2. You are given an array of prices where prices[i] is the price of a given stock o...read more
A Software Developer was asked 10mo agoQ3. Given a sorted array of integers nums and an integer target, write a function th...read more
Urban Company Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits