# Toggle all even bits of a number

Given a number, the task is to Toggle all even bit of a number

Examples:

```Input : 10
Output : 0
binary representation 1 0 1 0
after toggle          0 0 0 0

Input : 20
Output : 30
binary representation 1 0 1 0 0
after toggle          1 1 1 1 0
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

1. First generate a number that contains even position bits.
2. Take XOR with the original number. Note that 1 ^ 1 = 0 and 1 ^ 0 = 1.

Let’s understand this approach with below code.

## C++

 `// CPP code to Toggle all even ` `// bit of a number ` `#include ` `using` `namespace` `std; ` ` `  `// Returns a number which has all even ` `// bits of n toggled. ` `int` `evenbittogglenumber(``int` `n) ` `{ ` `    ``// Genarate number form of 101010 ` `    ``// ..till of same order as n ` `    ``int` `res = 0, count = 0; ` `    ``for` `(``int` `temp = n; temp > 0; temp >>= 1) { ` ` `  `        ``// if bit is even then generate ` `        ``// number and or with res ` `        ``if` `(count % 2 == 1) ` `            ``res |= (1 << count);       ` ` `  `        ``count++; ` `    ``} ` ` `  `    ``// return toggled number ` `    ``return` `n ^ res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 11; ` `    ``cout << evenbittogglenumber(n); ` `    ``return` `0; ` `} `

## Java

 `// Java code to Toggle all ` `// even bit of a number ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``// Returns a number which has ` `    ``// all even bits of n toggled. ` `    ``static` `int` `evenbittogglenumber(``int` `n) ` `    ``{ ` `        ``// Genarate number form of 101010 ` `        ``// ..till of same order as n ` `        ``int` `res = ``0``, count = ``0``; ` `        ``for` `(``int` `temp = n; temp > ``0``;  ` `                               ``temp >>= ``1``) ` `        ``{ ` `            ``// if bit is even then generate ` `            ``// number and or with res ` `            ``if` `(count % ``2` `== ``1``) ` `                ``res |= (``1` `<< count);       ` `      `  `            ``count++; ` `        ``} ` `      `  `        ``// return toggled number ` `        ``return` `n ^ res; ` `    ``} ` `      `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``11``; ` `        ``System.out.println(evenbittogglenumber(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Nikita Tiwari. `

## Python3

 `# Python code to Toggle all ` `# even bit of a number ` ` `  `# Returns a number which has all even ` `# bits of n toggled. ` `def` `evenbittogglenumber(n) : ` ` `  `    ``# Genarate number form of 101010 ` `    ``# ..till of same order as n ` `    ``res ``=` `0` `    ``count ``=` `0` `    ``temp ``=` `n ` ` `  `    ``while` `(temp > ``0``) : ` `         `  `        ``# if bit is even then generate ` `        ``# number and or with res ` `        ``if` `(count ``%` `2` `=``=` `1``) : ` `            ``res ``=` `res | (``1` `<< count)      ` `  `  `        ``count ``=` `count ``+` `1` `        ``temp >>``=` `1` `         `  `  `  `    ``# return toggled number ` `    ``return` `n ^ res ` `  `  `# Driver code ` `n ``=` `11` `print``(evenbittogglenumber(n)) ` ` `  `#This code is contributed by Nikita Tiwari. `

## C#

 `// C# code to Toggle all ` `// even bit of a number ` `using` `System; ` `  `  `class` `GFG { ` `      `  `    ``// Returns a number which has ` `    ``// all even bits of n toggled. ` `    ``static` `int` `evenbittogglenumber(``int` `n) ` `    ``{ ` `        ``// Genarate number form of 101010 ` `        ``// ..till of same order as n ` `        ``int` `res = 0, count = 0; ` `         `  `        ``for` `(``int` `temp = n; temp > 0;  ` `                               ``temp >>= 1) ` `        ``{ ` `            ``// if bit is even then generate ` `            ``// number and or with res ` `            ``if` `(count % 2 == 1) ` `                ``res |= (1 << count);       ` `       `  `            ``count++; ` `        ``} ` `       `  `        ``// return toggled number ` `        ``return` `n ^ res; ` `    ``} ` `       `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `         `  `        ``int` `n = 11; ` `         `  `        ``Console.WriteLine(evenbittogglenumber(n)); ` `    ``} ` `} ` `  `  `// This code is contributed by Anant Agarwal. `

## PHP

 ` 0; ``\$temp` `>>= 1)  ` `    ``{ ` ` `  `        ``// if bit is even then generate ` `        ``// number and or with res ` `        ``if` `(``\$count` `% 2 == 1) ` `            ``\$res` `|= (1 << ``\$count``);      ` ` `  `        ``\$count``++; ` `    ``} ` ` `  `    ``// return toggled number ` `    ``return` `\$n` `^ ``\$res``; ` `} ` ` `  `    ``// Driver code ` `    ``\$n` `= 11; ` `    ``echo` `evenbittogglenumber(``\$n``); ` ` `  `// This code is contributed by mits  ` `?> `

Output:

```1
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.