Toggle all even bits of a number - GeeksforGeeks

07 Sep, 2021
Given a number, the task is to Toggle all even bit of a numberExamples:  
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
 
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++
Java
Python3
C#
PHP
Javascript
// CPP code to Toggle all even// bit of a number#include <iostream>using namespace std; // Returns a number which has all even// bits of n toggled.int evenbittogglenumber(int n){    // Generate 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 codeint main(){    int n = 11;    cout << evenbittogglenumber(n);    return 0;}
// Java code to Toggle all// even bit of a numberimport java.io.*; class GFG {         // Returns a number which has    // all even bits of n toggled.    static int evenbittogglenumber(int n)    {        // Generate 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.
# Python code to Toggle all# even bit of a number # Returns a number which has all even# bits of n toggled.def evenbittogglenumber(n) :     # Generate 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 coden = 11print(evenbittogglenumber(n)) #This code is contributed by Nikita Tiwari.
// C# code to Toggle all// even bit of a numberusing System;  class GFG {          // Returns a number which has    // all even bits of n toggled.    static int evenbittogglenumber(int n)    {        // Generate 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// php code to Toggle all// even bit of a number // Returns a number which has// all even bits of n toggled.function evenbittogglenumber($n){         // Generate number form of 101010    // ..till of same order as n    $res = 0;    $count = 0;    for ($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    $n = 11;    echo evenbittogglenumber($n); // This code is contributed by mits?>
<script> // JavaScript program to Toggle all// even bit of a number     // Returns a number which has    // all even bits of n toggled.    function evenbittogglenumber(n)    {        // Generate number form of 101010        // ..till of same order as n        let res = 0, count = 0;                   for (let 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     let n = 11;                   document.write(evenbittogglenumber(n)); </script>
Output: 
1
 
Mithun Kumar
susmitakundugoaldanga
anikaseth98
Bitwise-XOR
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Program to find whether a given number is power of 2
Add two numbers without using arithmetic operators
Set, Clear and Toggle a given bit of a number in C
Josephus problem | Set 1 (A O(n) Solution)
Bit Fields in C
Find the element that appears once
Bits manipulation (Important tactics)