📜  交换所有奇数和偶数位

📅  最后修改于: 2021-04-27 18:33:12             🧑  作者: Mango

给定一个无符号整数,将所有奇数位与偶数位交换。例如,如果给定的数字为23( 0 0 0 1 0 1 1 1),则应将其转换为43(0 0 1 0 1 0 1 1 )。每个偶数位置位都与右侧的相邻位交换(偶数位置位以23的二进制表示形式突出显示),每个奇数位置位均与左侧的相邻位交换。

如果我们仔细查看该示例,可以观察到,我们基本上需要将所有偶数位(>>在上面的示例中,23的偶数位都突出显示)右移(>>),以使它们变为奇数位(突出显示)在43中),然后将所有奇数位左移(<<)1,以使它们变为偶数位。基于此观察结果,以下解决方案。该解决方案假定输入数字使用32位存储。
设输入数字为x
1)通过按位执行x的所有偶数位,并用0xAAAAAAAA执行x的所有偶数位。数字0xAAAAAAAA是32位数字,所有偶数位均设置为1,所有奇数位均设置为0。
2)通过按位执行x的所有奇数位,并使用0x55555555获得x的所有奇数位。数字0x55555555是32位数字,所有奇数位均设置为1,所有偶数位均设置为0。
3)右移所有偶数位。
4)将所有奇数位左移。
5)合并新的偶数和奇数位并返回。

C++
// C++ program to swap even and
// odd bits of a given number
#include 
using namespace std;
 
// Function to swap even
// and odd bits
unsigned int swapBits(unsigned int x)
{
    // Get all even bits of x
    unsigned int even_bits = x & 0xAAAAAAAA;
 
    // Get all odd bits of x
    unsigned int odd_bits = x & 0x55555555;
 
    even_bits >>= 1; // Right shift even bits
    odd_bits <<= 1; // Left shift odd bits
 
    return (even_bits | odd_bits); // Combine even and odd bits
}
 
// Driver code
int main()
{
    unsigned int x = 23; // 00010111
 
    // Output is 43 (00101011)
    cout<


C
// C program to swap even and
// odd bits of a given number
#include 
 
// Function to swap even
// and odd bits
unsigned int swapBits(unsigned int x)
{
    // Get all even bits of x
    unsigned int even_bits = x & 0xAAAAAAAA;
 
    // Get all odd bits of x
    unsigned int odd_bits  = x & 0x55555555;
 
    even_bits >>= 1;  // Right shift even bits
    odd_bits <<= 1;   // Left shift odd bits
 
    return (even_bits | odd_bits); // Combine even and odd bits
}
 
// Driver program to test above function
int main()
{
    unsigned int x = 23; // 00010111
 
    // Output is 43 (00101011)
    printf("%u ", swapBits(x));
 
    return 0;
}


Java
// Java program to swap even
// and odd bits of a given number
 
class GFG{
     
    // Function to swap even
    // and odd bits
    static int swapBits(int x)
    {
        // Get all even bits of x
        int even_bits = x & 0xAAAAAAAA;
     
        // Get all odd bits of x
        int odd_bits = x & 0x55555555;
     
        // Right shift even bits
        even_bits >>= 1;
         
        // Left shift even bits
        odd_bits <<= 1;
         
        // Combine even and odd bits
        return (even_bits | odd_bits);
    }
     
    // Driver program to test above function
    public static void main(String[] args)
    {
        int x = 23; // 00010111
     
        // Output is 43 (00101011)
        System.out.println(swapBits(x));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal


Python 3
# Python 3 program to swap even
# and odd bits of a given number
 
# Function for swapping even
# and odd bits
def swapBits(x) :
     
    # Get all even bits of x
    even_bits = x & 0xAAAAAAAA
 
    # Get all odd bits of x
    odd_bits = x & 0x55555555
     
    # Right shift even bits
    even_bits >>= 1
     
    # Left shift odd bits
    odd_bits <<= 1
 
    # Combine even and odd bits
    return (even_bits | odd_bits)
 
 
# Driver program
# 00010111
x = 23
 
# Output is 43 (00101011)
print(swapBits(x))
 
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to swap even and odd bits
// of a given number
using System;
 
class GFG {
     
    // Function to swap even
    // and odd bits
    static long swapBits(int x)
    {
        // Get all even bits of x
        long even_bits = x & 0xAAAAAAAA;
     
        // Get all odd bits of x
        long odd_bits = x & 0x55555555;
     
        // Right shift even bits
        even_bits >>= 1;
         
        // Left shift even bits
        odd_bits <<= 1;
         
        // Combine even and odd bits
        return (even_bits | odd_bits);
    }
     
    // Driver program to test above function
    public static void Main()
    {
         
        int x = 23; // 00010111
     
        // Output is 43 (00101011)
        Console.Write(swapBits(x));
    }
}
 
// This code is contributed by Sam007.


PHP
>= 1;
     
    // Left shift odd bits
    $odd_bits <<= 1;
 
    // Combine even and odd bits
    return ($even_bits | $odd_bits);
}
 
// Driver Code
 
// 00010111
$x = 23;
 
// Output is 43 (00101011)
echo swapBits($x);
 
// This code is contributed by Ajit
?>


Javascript


输出:

43