Alternate bits of two numbers to create a new number

Given two numbers, the task is to use alternative bits within two numbers to create result. We take first bits of second number, then second bit of the first number, third bit of second number and take the fourth bit of a first number and so on and generate a number with it.

Examples :

Input : n = 10, m = 11
Output : 11
Start from right of second number  
Binary representation of n = 1 0 1 0 
                             ^   ^     
Binary representation of m = 1 0 1 1     
                               ^   ^
Output  is                 = 1 0 1 1        

Input : n = 20, m = 7
Output : 5
Start from right of second number  
binary representation of n = 1 0 1 0 0
                           ^   ^
binary representation of m = 0 0 1 1 1
                         ^   ^   ^
Output  is                 = 0 0 1 0 1   



Approach :-
1. Get the set even bits number of n.
2. Get the set odd bits number of m.
3. return OR of these number.

Alternate_bits

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to generate a number using
// alternate bits of two numbers.
#include <iostream>
using namespace std;
  
// set even bit of number n
int setevenbits(int n)
{
    int temp = n;
    int count = 0;
  
    // res for store 101010.. number
    int res = 0;
  
    // generate number form of 101010.....
    // till temp size
    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 set even bit number
    return (n & res);
}
  
// set odd bit of number m
int setoddbits(int m)
{
    int count = 0;
  
    // res for store 101010.. number
    int res = 0;
  
    // generate number form of 101010....
    // till temp size
    for (int temp = m; temp > 0; temp >>= 1) {
  
        // if bit is even then generate
        // number and or with res
        if (count % 2 == 0)
            res |= (1 << count);
  
        count++;
    }
  
    // return set odd bit number
    return (m & res);
}
  
int getAlternateBits(int n, int m)
{
    // set even bit of number n
    int tempn = setevenbits(n);
  
    // set odd bit of number m
    int tempm = setoddbits(m);
  
    // take OR with these number
    return (tempn | tempm);
}
  
// Driver code
int main()
{
    int n = 10;
    int m = 11;
  
    // n  = 1 0 1 0
    //        ^   ^
    // m  = 1 0 1 1
    //          ^   ^
    // result= 1 0 1 1
  
    cout << getAlternateBits(n, m);
  
    return 0; 
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java Program to generate a number using
// alternate bits of two numbers.
import java.io.*;
  
class GFG {
      
    // set even bit of number n
    static int setevenbits(int n)
    {
      
        int temp = n;
        int count = 0;
      
        // res for store 101010.. number
        int res = 0;
      
        // generate number form of 101010.....
        // till temp size
        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 set even bit number
        return (n & res);
    }
  
    // set odd bit of number m
    static int setoddbits(int m)
    {
        int count = 0;
      
        // res for store 101010.. number
        int res = 0;
      
        // generate number form of 101010....
        // till temp size
        for (int temp = m; temp > 0; temp >>= 1)
        {
            // if bit is even then generate
            // number and or with res
            if (count % 2 == 0)
                res |= (1 << count);
      
            count++;
        }
      
        // return set odd bit number
        return (m & res);
    }
  
    static int getAlternateBits(int n, int m)
    {
        // set even bit of number n
        int tempn = setevenbits(n);
      
        // set odd bit of number m
        int tempm = setoddbits(m);
      
        // take OR with these number
        return (tempn | tempm);
    }
  
    // Driver code
    public static void main (String[] args) 
    {
      
    int n = 10;
    int m = 11;
  
    // n = 1 0 1 0
    //       ^   ^
    // m = 1 0 1 1
    //         ^   ^
    // result= 1 0 1 1
    System.out.println(getAlternateBits(n, m));
    }
}
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to generate a number using
# alternate bits of two numbers.
  
# set even bit of number n
def setevenbits(n):
    temp = n
    count = 0
  
    # res for store 101010.. number
    res = 0
  
    # generate number form of 101010.....
    # till temp size
    while temp > 0:
  
        # if bit is even then generate
        # number and or with res
        if count % 2:
            res |= (1 << count)
  
        count += 1
        temp >>= 1
  
    # return set even bit number
    return (n & res)
  
# set odd bit of number m
def setoddbits(m):
    temp = m
    count = 0
      
    # res for store 101010.. number
    res = 0
      
    # generate number form of 101010....
    # till temp size
    while temp > 0:
      
        # if bit is even then generate
        # number and or with res
        if not count % 2:
            res |= (1 << count)
      
        count += 1
        temp >>= 1
      
    # return set odd bit number
    return (m & res)
  
def getAlternateBits(n, m):
    # set even bit of number n
    tempn = setevenbits(n)
  
    # set odd bit of number m
    tempm = setoddbits(m)
  
    # take OR with these number
    return (tempn | tempm)
  
# Driver code
n = 10
m = 11
  
# n = 1 0 1 0
#     ^ ^
# m = 1 0 1 1
#         ^ ^
# result= 1 0 1 1
  
print(getAlternateBits(n, m))
  
# This code is contributed by Ansu Kumari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to generate a number using
// alternate bits of two numbers.
using System;
  
class GFG {
      
    // set even bit of number n
    static int setevenbits(int n)
    {
      
        int temp = n;
        int count = 0;
      
        // res for store 101010.. number
        int res = 0;
      
        // generate number form of 101010.....
        // till temp size
        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 set even bit number
        return (n & res);
    }
  
    // set odd bit of number m
    static int setoddbits(int m)
    {
        int count = 0;
      
        // res for store 101010.. number
        int res = 0;
      
        // generate number form of 101010....
        // till temp size
        for (int temp = m; temp > 0; temp >>= 1)
        {
            // if bit is even then generate
            // number and or with res
            if (count % 2 == 0)
                res |= (1 << count);
      
            count++;
        }
      
        // return set odd bit number
        return (m & res);
    }
  
    static int getAlternateBits(int n, int m)
    {
        // set even bit of number n
        int tempn = setevenbits(n);
      
        // set odd bit of number m
        int tempm = setoddbits(m);
      
        // take OR with these number
        return (tempn | tempm);
    }
  
    // Driver code
    public static void Main () 
    {
      
        int n = 10;
        int m = 11;
      
        // n = 1 0 1 0
        // ^ ^
        // m = 1 0 1 1
        //     ^ ^
        // result= 1 0 1 1
        Console.WriteLine(getAlternateBits(n, m));
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to generate a number using
// alternate bits of two numbers.
  
// set even bit of number n
function setevenbits($n)
{
    $temp = $n;
    $count = 0;
  
    // res for store 101010.. number
    $res = 0;
  
    // generate number form of 101010.....
    // till temp size
    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 set even bit number
    return ($n & $res);
}
  
// set odd bit of number m
function setoddbits($m)
{
    $count = 0;
  
    // res for store 101010.. number
    $res = 0;
  
    // generate number form of 101010....
    // till temp size
    for ($temp = $m; $temp > 0; $temp >>= 1) 
    {
  
        // if bit is even then generate
        // number and or with res
        if ($count % 2 == 0)
            $res |= (1 << $count);
  
        $count++;
    }
  
    // return set odd bit number
    return ($m & $res);
}
  
function getAlternateBits($n, $m)
{
    // set even bit of number n
    $tempn = setevenbits($n);
  
    // set odd bit of number m
    $tempm = setoddbits($m);
  
    // take OR with these number
    return ($tempn | $tempm);
}
  
// Driver code
$n = 10;
$m = 11;
  
// n = 1 0 1 0
// ^ ^
// m = 1 0 1 1
//     ^ ^
// result= 1 0 1 1
  
echo getAlternateBits($n, $m);
  
// This code is contributed by mits 
?>

chevron_right


Output :

11


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.