Posted on by Kalkicode
Code Bit Logic

# Toggle all even bits of a number

Computers represent numbers in binary format using bits (0s and 1s). Each bit position in the binary representation of a number has a specific significance. In this problem, we are given a number and our task is to toggle (flip) all the even-positioned bits of the number. For example, if the binary representation of the number is 101001, after toggling the even bits, it becomes 111011.

Let's understand the process of toggling even bits step by step:

1. Introduction: In this problem, we are given a positive integer 'num' and our goal is to toggle all the even bits of this number. We will do this using bitwise operations in an efficient manner.
2. Problem Statement: Given an integer 'num', we need to toggle all its even bits, i.e., bits at positions 2, 4, 6, and so on (0-indexed).
3. Explanation with Example: Let's consider the number 16 (binary 10000). The even bits in the binary representation are 0, 0, and 1. After toggling these bits, the number becomes 26 (binary 11010).
4. Pseudocode: We will use a loop to iterate through each even-positioned bit and toggle it using bitwise XOR (^) operation.

The pseudocode for toggling all even bits of a number 'num' is as follows:

``````
Procedure changeEvenBit(num):
n = num
start = 2
while n >= start:
n = n XOR start
start = start << 2
Display "Number:", num
Display "After Change:", n
End Procedure
```
```

Algorithm:

1. Start with the given number 'num' and initialize a variable 'n' to store the modified number.
2. Set 'start' to 2, which represents the position of the first even bit (bit at position 2).
3. While 'n' is greater than or equal to 'start', do the following:
• Toggle the bit at the even position using the XOR operation between 'n' and 'start', and store the result in 'n'.
• Update 'start' to the next even position by shifting it left by 2 bits (equivalent to multiplying by 4).
4. After the loop ends, the variable 'n' will contain the number with all even bits toggled.
5. Display the original number 'num' and the modified number 'n'.

Output Explanation:

When we run the code with 'changeEvenBit(16)', the original number is 16 (binary 10000). After toggling the even bits (bit at position 2), the number becomes 26 (binary 11010). The same process applies when we run the code with 'changeEvenBit(123)'. The original number is 123 (binary 1111011), and after toggling the even bits (bits at positions 2, 4, and 6), the number becomes 81 (binary 1010001).

## Code Solution

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)
{
// (16) 10000 => 11010 (26)
// (123) 1111011 => 1010001 (81) [Change even position]
}
}``````

#### 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()
{
// (16) 10000 => 11010 (26)
// (123) 1111011 => 1010001 (81) [Change even position]
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)
{
// (16) 10000 => 11010 (26)
// (123) 1111011 => 1010001 (81) [Change even position]
}
}``````

#### 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()
{
// (16) 10000 => 11010 (26)
// (123) 1111011 => 1010001 (81) [Change even position]
}
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()
{
// (16) 10000 => 11010 (26)
// (123) 1111011 => 1010001 (81) [Change even position]
}
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() :
#  (16) 10000 => 11010 (26)
#  (123) 1111011 => 1010001 (81) [Change even position]

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()
#  (16) 10000 => 11010 (26)
#  (123) 1111011 => 1010001 (81) [Change even position]
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)
// (123) 1111011 => 1010001 (81) [Change even position]
}
}``````

#### 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()
{
// (16) 10000 => 11010 (26)
// (123) 1111011 => 1010001 (81) [Change even position]
}
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
{
// (16) 10000 => 11010 (26)
// (123) 1111011 => 1010001  (81) [Change even position]
}``````

#### Output

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

Time Complexity Analysis:

The time complexity of this algorithm is O(log N), where N is the given number. The loop runs for each even bit position, and there are log(N) even bits in a number with N bits. Hence, the time complexity is logarithmic in the number of bits of the input.

Finally, we have discussed how to toggle all even bits of a given number using bitwise operations. The algorithm efficiently processes the bits and gives the modified number with toggled even bits. By following the pseudocode and understanding the steps, we can easily implement this logic in any programming language.

## Comment

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.

Categories
Relative Post