Posted on by Kalkicode
Code Bit Logic

# Change all odd position bit of a number

In computer systems, data is represented using binary digits, or bits, which can be either 0 or 1. Sometimes, there arises a need to manipulate certain bits of a given number based on their position. In this article, we will focus on changing all odd position bits of a number, leaving the even position bits unchanged.

## Problem Statement

Given a positive integer 'num,' the task is to change all the bits at odd positions (1st, 3rd, 5th, etc.) of the binary representation of 'num' and display the resultant number.

## Explanation with Suitable Example

Let's consider the number '16' (binary: 10000) and '123' (binary: 1111011) as examples to demonstrate the process of changing odd position bits.

1. For '16' (binary: 10000), we have to change the odd-position bits. After the transformation, the new binary representation would be 0101, which is '5' in decimal.

2. For '123' (binary: 1111011), we have to change the odd-position bits. After the transformation, the new binary representation would be 0101110, which is '46' in decimal.

## Pseudocode

To change all odd position bits of a given number, we can follow the steps below:

1. Initialize two variables: 'n' as the input number and 'start' as 1.
2. Iterate while 'n' is greater than or equal to 'start': a. XOR 'n' with 'start' to change the current odd-position bit. b. Left-shift 'start' by 2 positions to target the next odd-position bit.
3. The final value of 'n' after the loop represents the number with changed odd-position bits.

## Algorithm with Explanation

Let's delve into the algorithm step by step:

1. Start with the input number 'num' and initialize 'n' as 'num' to avoid modifying the original value.
2. Initialize 'start' as 1. The 'start' will be used to set or unset the bits at odd positions. It will start with the least significant bit (position 0) and move to higher odd positions (1st, 3rd, 5th, and so on).
3. Enter a loop that continues as long as 'n' is greater than or equal to 'start'. This ensures we don't process more bits than required, as the number 'n' will eventually become smaller than 'start' due to the left-shift operation in the loop.
4. Inside the loop, perform an XOR operation between 'n' and 'start'. XORing a bit with 1 toggles it (changes 0 to 1 and 1 to 0), but it leaves even bits (bits at even positions) unchanged since XORing with 0 doesn't change the bit.
5. After the XOR operation, left-shift 'start' by 2 positions. This moves 'start' to the next odd position, so it's ready for the next iteration of the loop.
6. Repeat steps 4 and 5 until the loop condition ('n' >= 'start') becomes false.
7. The final value of 'n' represents the number with changed odd-position bits. Display this value as the output.

## Code Solution

Here given code implementation process.

``````// C Program
// Change all odd bits of a number
#include <stdio.h>

// Change odd position bit of a given number
void changeOddBit(int num)
{
int n = num;
int start = 1;
while (n >= start)
{
// Change bits
n = start ^ n;
// Shift bits to left by 2 bit
start = start << 2;
}
// Display calculated result
printf("\n Number : %d", num);
printf("\n After Change Odd Position Bits : %d", n);
}
int main()
{
// 1000 => 0101
// Change odd position bit
changeOddBit(16);
// 1111011 => 0101110 [Change Odd position]
changeOddBit(123);
return 0;
}``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````/*
Java Program for
Change all odd bits of a number
*/
class SwitchBit
{
// Change odd position bit of a given number
public void changeOddBit(int num)
{
int n = num;
int start = 1;
while (n >= start)
{
// Change bits
n = start ^ n;
// Shift bits to left by 2 bit
start = start << 2;
}
// Display calculated result
System.out.print("\n Number : " + num);
System.out.print("\n After Change Odd Position Bits : " + n);
}
public static void main(String[] args)
{
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
}
}``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program for
Change all odd bits of a number
*/

class SwitchBit
{
public:
// Change odd position bit of a given number
void changeOddBit(int num)
{
int n = num;
int start = 1;
while (n >= start)
{
// Change bits
n = start ^ n;
// Shift bits to left by 2 bit
start = start << 2;
}
// Display calculated result
cout << "\n Number : " << num;
cout << "\n After Change Odd Position Bits : " << n;
}
};
int main()
{
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
return 0;
}``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````// Include namespace system
using System;
/*
C# Program for
Change all odd bits of a number
*/
public class SwitchBit
{
// Change odd position bit of a given number
public void changeOddBit(int num)
{
int n = num;
int start = 1;
while (n >= start)
{
// Change bits
n = start ^ n;
// Shift bits to left by 2 bit
start = start << 2;
}
// Display calculated result
Console.Write("\n Number : " + num);
Console.Write("\n After Change Odd Position Bits : " + n);
}
public static void Main(String[] args)
{
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
}
}``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````<?php
/*
Php Program for
Change all odd bits of a number
*/
class SwitchBit
{
// Change odd position bit of a given number
public	function changeOddBit(\$num)
{
\$n = \$num;
\$start = 1;
while (\$n >= \$start)
{
// Change bits
\$n = \$start ^ \$n;
// Shift bits to left by 2 bit
\$start = \$start << 2;
}
// Display calculated result
echo "\n Number : ". \$num;
echo "\n After Change Odd Position Bits : ". \$n;
}
}

function main()
{
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
}
main();``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````/*
Node Js Program for
Change all odd bits of a number
*/
class SwitchBit
{
// Change odd position bit of a given number
changeOddBit(num)
{
var n = num;
var start = 1;
while (n >= start)
{
// Change bits
n = start ^ n;
// Shift bits to left by 2 bit
start = start << 2;
}
// Display calculated result
process.stdout.write("\n Number : " + num);
process.stdout.write("\n After Change Odd Position Bits : " + n);
}
}

function main()
{
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
}
main();``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````#   Python 3 Program for
#   Change all odd bits of a number

class SwitchBit :
#  Change odd position bit of a given number
def changeOddBit(self, num) :
n = num
start = 1
while (n >= start) :
#  Change bits
n = start ^ n
#  Shift bits to left by 2 bit
start = start << 2

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

def main() :
#  1000 => 0101
#  Change odd position bit
#  1111011 => 0101110 [Change Odd position]

if __name__ == "__main__": main()``````

#### Output

`````` Number :  16
After Change Odd Position Bits :  5
Number :  123
After Change Odd Position Bits :  46``````
``````#   Ruby Program for
#   Change all odd bits of a number

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

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

end

def main()
#  1000 => 0101
#  Change odd position bit
#  1111011 => 0101110 [Change Odd position]
end

main()``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````/*
Scala Program for
Change all odd bits of a number
*/
class SwitchBit
{
// Change odd position bit of a given number
def changeOddBit(num: Int): Unit = {
var n: Int = num;
var start: Int = 1;
while (n >= start)
{
// Change bits
n = start ^ n;
// Shift bits to left by 2 bit
start = start << 2;
}
// Display calculated result
print("\n Number : " + num);
print("\n After Change Odd Position Bits : " + n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SwitchBit = new SwitchBit();
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
}
}``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````
``````/*
Swift 4 Program for
Change all odd bits of a number
*/
class SwitchBit
{
// Change odd position bit of a given number
func changeOddBit(_ num: Int)
{
var n: Int = num;
var start: Int = 1;
while (n >= start)
{
// Change bits
n = start ^ n;
// Shift bits to left by 2 bit
start = start << 2;
}
// Display calculated result
print("\n Number : ", num, terminator: "");
print("\n After Change Odd Position Bits : ", n, terminator: "");
}
}
func main()
{
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
}
main();``````

#### Output

`````` Number :  16
After Change Odd Position Bits :  5
Number :  123
After Change Odd Position Bits :  46``````
``````/*
Kotlin Program for
Change all odd bits of a number
*/
class SwitchBit
{
// Change odd position bit of a given number
fun changeOddBit(num: Int): Unit
{
var n: Int = num;
var start: Int = 1;
while (n >= start)
{
// Change bits
n = start xor n;
// Shift bits to left by 2 bit
start = start shl 2;
}
// Display calculated result
print("\n Number : " + num);
print("\n After Change Odd Position Bits : " + n);
}
}
fun main(args: Array < String > ): Unit
{
// 1000 => 0101
// Change odd position bit
// 1111011 => 0101110 [Change Odd position]
}``````

#### Output

`````` Number : 16
After Change Odd Position Bits : 5
Number : 123
After Change Odd Position Bits : 46``````

## Resultant Output Explanation by Statement

Let's analyze the output of the given code for the example numbers '16' and '123':

1. For 'num = 16':

• The binary representation of 16 is 10000.
• The loop runs twice because 'n' becomes smaller than 'start' after the second iteration (n=1, start=4).
• After the first iteration, 'n' becomes 10000 XOR 00001 = 10001 (decimal 17).
• After the second iteration, 'n' becomes 10001 XOR 00100 = 10101 (decimal 21).
• The final value of 'n' is 10101 (decimal 21).
• Output: "Number: 16, After Change Odd Position Bits: 21."
2. For 'num = 123':

• The binary representation of 123 is 1111011.
• The loop runs three times because 'n' becomes smaller than 'start' after the third iteration (n=3, start=4).
• After the first iteration, 'n' becomes 1111011 XOR 00001 = 1111010 (decimal 122).
• After the second iteration, 'n' becomes 1111010 XOR 00100 = 1111110 (decimal 126).
• After the third iteration, 'n' becomes 1111110 XOR 10000 = 1011110 (decimal 46).
• The final value of 'n' is 1011110 (decimal 46).
• Output: "Number: 123, After Change Odd Position Bits: 46."

## Time Complexity of the Code

The time complexity of the given code is O(log(num)) because the loop iterates log(num) times, where 'num' is the input number. In each iteration, we perform constant time operations (XOR and left-shift), making the overall complexity logarithmic.

This article discussed how to change all odd position bits of a number using the XOR operation and left-shifting. We provided a step-by-step explanation of the algorithm and demonstrated its implementation on two example numbers. The code's time complexity was also analyzed to understand its efficiency. By understanding this process, readers can better manipulate individual bits of numbers in various programming scenarios.

## 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