Given a value X, the task is to find the number Y which will give maximum value possible when XOR with X.

(Assume X to be 8 bits) Maximum possible value of X and Y both is 255.

**Examples:**

Input:X = 2Output:253 Binary Representation of X = 00000010 Binary Representation of Y = 11111101 Maximum XOR value: 11111111Input:X = 200Output:55

**Approach:** In order to ensure maximum value of XOR, we need to set all those bits ON which are OFF in X. So for that, Y should have all bits ON which are OFF in X and OFF which are ON in X as (0^1 = 1 and 1^0 = 1). So Y should be the 1’s complement representation of X.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function To Calculate Answer ` `int` `calculate(` `int` `X) ` `{ ` ` ` `// Find number of bits in the given integer ` ` ` `int` `number_of_bits = 8; ` ` ` ` ` `// XOR the given integer with poe(2, ` ` ` `// number_of_bits-1 and print the result ` ` ` `return` `((1 << number_of_bits) - 1) ^ X; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `X = 4; ` ` ` ` ` `cout << ` `"Required Number is : "` ` ` `<< calculate(X) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function To Calculate Answer ` `static` `int` `calculate(` `int` `X) ` `{ ` ` ` `// Find number of bits in the given integer ` ` ` `int` `number_of_bits = ` `8` `; ` ` ` ` ` `// XOR the given integer with poe(2, ` ` ` `// number_of_bits-1 and print the result ` ` ` `return` `((` `1` `<< number_of_bits) - ` `1` `) ^ X; ` `} ` ` ` `// Driver Code ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `X = ` `4` `; ` ` ` ` ` `System.out.println( ` `"Required Number is : "` ` ` `+calculate(X)); ` ` ` `} ` `} ` `// This code is contributed by shs.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function To Calculate Answer ` `def` `calculate(X): ` ` ` ` ` `# Find number of bits in the given integer ` ` ` `number_of_bits ` `=` `8` ` ` ` ` `# XOR the given integer with poe(2, ` ` ` `# number_of_bits-1 and print the result ` ` ` `return` `((` `1` `<< number_of_bits) ` `-` `1` `) ^ X ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `X ` `=` `4` ` ` `print` `(` `"Required Number is:"` `, calculate(X)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function To Calculate Answer ` `static` `int` `calculate(` `int` `X) ` `{ ` ` ` `// Find number of bits in ` ` ` `// the given integer ` ` ` `int` `number_of_bits = 8; ` ` ` ` ` `// XOR the given integer with poe(2, ` ` ` `// number_of_bits-1 and print the result ` ` ` `return` `((1 << number_of_bits) - 1) ^ X; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `X = 4; ` ` ` ` ` `Console.WriteLine(` `"Required Number is : "` `+ ` ` ` `calculate(X)); ` `} ` `} ` ` ` `// This code is contributed by shs.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the ` `// above approach ` ` ` `// Function To Calculate Answer ` `function` `calculate(` `$X` `) ` `{ ` ` ` `// Find number of bits in ` ` ` `// the given integer ` ` ` `$number_of_bits` `= 8; ` ` ` ` ` `// XOR the given integer with ` ` ` `// poe(2, number_of_bits-1 and ` ` ` `// print the result ` ` ` `return` `((1 << ` `$number_of_bits` `) - 1) ^ ` `$X` `; ` `} ` ` ` `// Driver Code ` `$X` `= 4; ` ` ` `echo` `"Required Number is : "` `. ` ` ` `calculate(` `$X` `) . ` `"\n"` `; ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Required Number is : 251

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the node whose xor with x gives maximum value
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Number whose XOR sum with given array is a given number k
- Number whose sum of XOR with given array range is maximum
- Find the node whose xor with x gives minimum value
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Maximum XOR with given value in the path from root to given node in the tree
- Find the root of the sub-tree whose weighted sum XOR with X is maximum
- Given a set, find XOR of the XOR's of all subsets.
- Find XOR of two number without using XOR operator
- Count smaller numbers whose XOR with n produces greater value
- Range Query on array whose each element is XOR of index value and previous element
- Length of longest subsequence whose XOR value is odd
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Find Maximum XOR value of a sub-array of size k
- Value in a given range with maximum XOR
- Find the root of the sub-tree whose weighted sum XOR with X is minimum
- Find an N x N grid whose xor of every row and column is equal
- Minimize K whose XOR with given array elements leaves array unchanged
- Find number of subarrays with XOR value a power of 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.