Toggle all even bits of a number

Here given code implementation process.

// C Program 
// Toggle all even bits of a number
#include <stdio.h>

// Change all even position bit of a given number
void changeEvenBit(int num)
{
    int n = num;
    // Frist number
    int start = 2;
    while (n >= start)
    {
        // Change bits
        n = start ^ n;
        // Shift bits to left by 2 bit
        start = start << 2;
    }
    // Display calculated result
    printf(" Number : %d", num);
    printf("\n After Change : %d\n", n);
}
int main()
{
    // (16) 10000 => 11010 (26)
    changeEvenBit(16);
    // (123) 1111011 => 1010001 (81) [Change even position]
    changeEvenBit(123);
    return 0;
}

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
/*
  Java Program for
  Toggle all even bits of a number
*/
public class SwitchBit
{
    // Change all even position bit of a given number
    public void changeEvenBit(int num)
    {
        int n = num;
        // Frist number
        int start = 2;
        while (n >= start)
        {
            // Change bits
            n = start ^ n;
            // Shift bits to left by 2 bit
            start = start << 2;
        }
        // Display calculated result
        System.out.print(" Number : " + num);
        System.out.print("\n After Change : " + n + "\n");
    }
    public static void main(String[] args)
    {
        SwitchBit task = new SwitchBit();
        // (16) 10000 => 11010 (26)
        task.changeEvenBit(16);
        // (123) 1111011 => 1010001 (81) [Change even position]
        task.changeEvenBit(123);
    }
}

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Toggle all even bits of a number
*/
class SwitchBit
{
    public:
        // Change all even position bit of a given number
        void changeEvenBit(int num)
        {
            int n = num;
            // Frist number
            int start = 2;
            while (n >= start)
            {
                // Change bits
                n = start ^ n;
                // Shift bits to left by 2 bit
                start = start << 2;
            }
            // Display calculated result
            cout << " Number : " << num;
            cout << "\n After Change : " << n << "\n";
        }
};
int main()
{
    SwitchBit task = SwitchBit();
    // (16) 10000 => 11010 (26)
    task.changeEvenBit(16);
    // (123) 1111011 => 1010001 (81) [Change even position]
    task.changeEvenBit(123);
    return 0;
}

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
// Include namespace system
using System;
/*
  C# Program for
  Toggle all even bits of a number
*/
public class SwitchBit
{
    // Change all even position bit of a given number
    public void changeEvenBit(int num)
    {
        int n = num;
        // Frist number
        int start = 2;
        while (n >= start)
        {
            // Change bits
            n = start ^ n;
            // Shift bits to left by 2 bit
            start = start << 2;
        }
        // Display calculated result
        Console.Write(" Number : " + num);
        Console.Write("\n After Change : " + n + "\n");
    }
    public static void Main(String[] args)
    {
        SwitchBit task = new SwitchBit();
        // (16) 10000 => 11010 (26)
        task.changeEvenBit(16);
        // (123) 1111011 => 1010001 (81) [Change even position]
        task.changeEvenBit(123);
    }
}

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
<?php
/*
  Php Program for
  Toggle all even bits of a number
*/
class SwitchBit
{
    // Change all even position bit of a given number
    public  function changeEvenBit($num)
    {
        $n = $num;
        // Frist number
        $start = 2;
        while ($n >= $start)
        {
            // Change bits
            $n = $start ^ $n;
            // Shift bits to left by 2 bit
            $start = $start << 2;
        }
        // Display calculated result
        echo " Number : ". $num;
        echo "\n After Change : ". $n ."\n";
    }
}

function main()
{
    $task = new SwitchBit();
    // (16) 10000 => 11010 (26)
    $task->changeEvenBit(16);
    // (123) 1111011 => 1010001 (81) [Change even position]
    $task->changeEvenBit(123);
}
main();

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
/*
  Node Js Program for
  Toggle all even bits of a number
*/
class SwitchBit
{
    // Change all even position bit of a given number
    changeEvenBit(num)
    {
        var n = num;
        // Frist number
        var start = 2;
        while (n >= start)
        {
            // Change bits
            n = start ^ n;
            // Shift bits to left by 2 bit
            start = start << 2;
        }
        // Display calculated result
        process.stdout.write(" Number : " + num);
        process.stdout.write("\n After Change : " + n + "\n");
    }
}

function main()
{
    var task = new SwitchBit();
    // (16) 10000 => 11010 (26)
    task.changeEvenBit(16);
    // (123) 1111011 => 1010001 (81) [Change even position]
    task.changeEvenBit(123);
}
main();

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
#   Python 3 Program for
#   Toggle all even bits of a number

class SwitchBit :
    #  Change all even position bit of a given number
    def changeEvenBit(self, num) :
        n = num
        #  Frist number
        start = 2
        while (n >= start) :
            #  Change bits
            n = start ^ n
            #  Shift bits to left by 2 bit
            start = start << 2
        
        #  Display calculated result
        print(" Number : ", num, end = "")
        print("\n After Change : ", n )
    

def main() :
    task = SwitchBit()
    #  (16) 10000 => 11010 (26)
    task.changeEvenBit(16)
    #  (123) 1111011 => 1010001 (81) [Change even position]
    task.changeEvenBit(123)

if __name__ == "__main__": main()

Output

 Number :  16
 After Change :  26
 Number :  123
 After Change :  81
#   Ruby Program for
#   Toggle all even bits of a number

class SwitchBit 
    #  Change all even position bit of a given number
    def changeEvenBit(num) 
        n = num
        #  Frist number
        start = 2
        while (n >= start) 
            #  Change bits
            n = start ^ n
            #  Shift bits to left by 2 bit
            start = start << 2
        end

        #  Display calculated result
        print(" Number : ", num)
        print("\n After Change : ", n ,"\n")
    end

end

def main() 
    task = SwitchBit.new()
    #  (16) 10000 => 11010 (26)
    task.changeEvenBit(16)
    #  (123) 1111011 => 1010001 (81) [Change even position]
    task.changeEvenBit(123)
end

main()

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
/*
  Scala Program for
  Toggle all even bits of a number
*/
class SwitchBit
{
    // Change all even position bit of a given number
    def changeEvenBit(num: Int): Unit = {
        var n: Int = num;
        // Frist number
        var start: Int = 2;
        while (n >= start)
        {
            // Change bits
            n = start ^ n;
            // Shift bits to left by 2 bit
            start = start << 2;
        }
        // Display calculated result
        print(" Number : " + num);
        print("\n After Change : " + n + "\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: SwitchBit = new SwitchBit();
        // (16) 10000 => 11010 (26)
        task.changeEvenBit(16);
        // (123) 1111011 => 1010001 (81) [Change even position]
        task.changeEvenBit(123);
    }
}

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81
/*
  Swift 4 Program for
  Toggle all even bits of a number
*/
class SwitchBit
{
    // Change all even position bit of a given number
    func changeEvenBit(_ num: Int)
    {
        var n: Int = num;
        // Frist number
        var start: Int = 2;
        while (n >= start)
        {
            // Change bits
            n = start ^ n;
            // Shift bits to left by 2 bit
            start = start << 2;
        }
        // Display calculated result
        print(" Number : ", num, terminator: "");
        print("\n After Change : ", n );
    }
}
func main()
{
    let task: SwitchBit = SwitchBit();
    // (16) 10000 => 11010 (26)
    task.changeEvenBit(16);
    // (123) 1111011 => 1010001 (81) [Change even position]
    task.changeEvenBit(123);
}
main();

Output

 Number :  16
 After Change :  26
 Number :  123
 After Change :  81
/*
  Kotlin Program for
  Toggle all even bits of a number
*/
class SwitchBit
{
    // Change all even position bit of a given number
    fun changeEvenBit(num: Int): Unit
    {
        var n: Int = num;
        // Frist number
        var start: Int = 2;
        while (n >= start)
        {
            // Change bits
            n = start xor n;
            // Shift bits to left by 2 bit
            start = start shl 2;
        }
        // Display calculated result
        print(" Number : " + num);
        print("\n After Change : " + n + "\n");
    }
}
fun main(args: Array < String > ): Unit
{
    var task: SwitchBit = SwitchBit();
    // (16) 10000 => 11010 (26)
    task.changeEvenBit(16);
    // (123) 1111011 => 1010001  (81) [Change even position]
    task.changeEvenBit(123);
}

Output

 Number : 16
 After Change : 26
 Number : 123
 After Change : 81


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved