📜  两个数字的交替位以创建新数字

📅  最后修改于: 2021-05-25 07:28:19             🧑  作者: Mango

给定两个数字,任务是使用两个数字中的替代位来创建结果。我们取第二个数字的第一位,然后取第一个数字的第二位,第二个数字的第三位,取第一个数字的第四位,依此类推,并由此生成一个数字。
例子 :

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   

方法 :-
1.获得n的设置偶数位数。
2.获得设置的奇数位数m。
3.返回这些数字的OR。

Alternate_bits

C++
// CPP Program to generate a number using
// alternate bits of two numbers.
#include 
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;
}


Java
// 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


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


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


PHP
 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
?>


Javascript


输出 :

11