Alternate bits of two numbers to create a new number in C++



Here, we are given two numbers, and we have to create a new number by combining their bits in an alternating way.

We take the first bit from the second number, the second bit from the first number, the third bit from the second again, and so on. We start from the least significant bit and continue until all bits from both numbers are used.

Let's look at a few example scenarios to understand the problem clearly:

Scenario 1:

Input: First_number = 8, Second_number = 9
Output: 9

Explanation:
Binary of 8 = 1000, Binary of 9 = 1001
Taking alternate bits:
1st bit -> from 9 -> 1 (result = 1)
2nd bit -> from 8 -> 0 (result = 01)
3rd bit -> from 9 -> 0 (result = 010)
4th bit -> from 8 -> 1 (result = 1001)
Formed bits: 1001 -> which gives 9

Scenario 2:

Input: First_number = 17, Second_number = 23
Output: 31

Explanation:
Binary of 17 = 10001, Binary of 23 = 10111
Taking alternate bits to create new number :
    17 = 1 0 0 0 1, 23 = 1 0 1 1 1
           ^   ^         ^   ^   ^
                1 0 1 0 1         
Formed bits: 10101 -> which gives 21

Alternate Bits to Create a New Number

To create a new number using alternate bits from two given numbers, we take even-positioned bits from the first number and odd-positioned bits from the second number. Then, we combine them using a bitwise OR operation.

Here are the steps we followed:

  • We start by initializing result = 0 to store the final combined number.
  • We pass the first number to the setevenbits() function. It checks each bit position from right to left using a loop. If the position is even (based on 0-based indexing, i.e., when count % 2 == 1), we keep that bit into the result using the bitwise OR operation.
  • Similarly, we pass the second number to the setoddbits() function. It loops through each bit, and if the position is odd (count % 2 == 0), we keep that bit in the result using bitwise OR.
  • Once we take the even bits from the first number and the odd bits from the second, we combine them using the bitwise OR operation to get a new number.

C++ Program to Combine Alternate Bits from Two Numbers

The following is the C++ program where we combine alternate bits from two numbers to form a new number, as explained above:

#include <iostream>
#include <bitset>
using namespace std;

// Function to keep only even-positioned bits (0-based indexing)
int setevenbits(int n){
   int temp = n;
   int count = 0;
   int res = 0;
   for (temp = n; temp > 0; temp >>= 1) {
      if (count % 2 == 1)  // even position (0-based)
         res |= (1 << count);
      count++;
   }
   return (n & res);
}

// Function to keep only odd-positioned bits (0-based indexing)
int setoddbits(int m){
   int count = 0;
   int res = 0;
   for (int temp = m; temp > 0; temp >>= 1) {
      if (count % 2 == 0)  // odd position (0-based)
         res |= (1 << count);
      count++;
   }
   return (m & res);
}

int main(){
   int n = 12; 
   int m = 17; 

   cout << "Input n = " << n << " -> " << bitset<8>(n) << endl;
   cout << "Input m = " << m << " -> " << bitset<8>(m) << endl;

   int setn = setevenbits(n);  // get even-positioned bits from n
   int setm = setoddbits(m);   // get odd-positioned bits from m
 
   int result = (setn | setm); // combine using bitwise OR

   cout << "Final Result after combining: " << bitset<8>(result) << " (" << result << ")" << endl;
   return 0;
}

Following is the output of the above program after combining even bits of the first number and odd bits of the second number-

Input n = 12 -> 00001100
Input m = 17 -> 00010001
Final Result after combining: 00011001 (25)

Time Complexity: O( log max(N, M)) because we iterate through the bits of the input numbers.

Space Complexity: O(1) because we use a constant amount of space.

Conclusion

In this article, we learned how to create a new number by taking even-positioned bits from one number and odd-positioned bits from another, using bitwise operations. The program runs in O(log max(m, n)) time and uses O(1) space.

Updated on: 2025-07-28T19:26:34+05:30

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements