📜  反转给定数字的实际位

📅  最后修改于: 2021-04-29 13:35:12             🧑  作者: Mango

给定一个非负整数n 。问题是要反转n位并打印反转位后获得的数字。请注意,正考虑将数字的实际二进制表示形式用于反转这些位,而不考虑前导0。
例子 :

Input : 11
Output : 13
(11)10 = (1011)2.
After reversing the bits we get:
(1101)2 = (13)10.

Input : 10
Output : 5
(10)10 = (1010)2.
After reversing the bits we get:
(0101)2 = (101)2
        = (5)10.

在这种方法中,借助于按位右移运算获得n的二进制表示形式的一个一位,并借助于按位左移运算以rev形式对其进行累加。
算法:

C++
// C++ implementation to reverse bits of a number
#include 
 
using namespace std;
 
// function to reverse bits of a number
unsigned int reverseBits(unsigned int n)
{
    unsigned int rev = 0;
     
    // traversing bits of 'n' from the right
    while (n > 0)
    {
        // bitwise left shift
        // 'rev' by 1
        rev <<= 1;
         
        // if current bit is '1'
        if (n & 1 == 1)
            rev ^= 1;
         
        // bitwise right shift
        // 'n' by 1
        n >>= 1;
             
    }
     
    // required number
    return rev;
}
 
// Driver program to test above
int main()
{
    unsigned int n = 11;
    cout << reverseBits(n);
    return 0;
}


Java
// Java implementation to
// reverse bits of a number
class GFG
{
    // function to reverse bits of a number
    public static int reverseBits(int n)
    {
        int rev = 0;
 
        // traversing bits of 'n'
        // from the right
        while (n > 0)
        {
            // bitwise left shift
            // 'rev' by 1
            rev <<= 1;
 
            // if current bit is '1'
            if ((int)(n & 1) == 1)
                rev ^= 1;
 
            // bitwise right shift
            //'n' by 1
            n >>= 1;
        }
        // required number
        return rev;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 11;
        System.out.println(reverseBits(n));
    }
}
 
// This code is contributed
// by prerna saini.


Python3
# Python 3 implementation to
# reverse bits of a number
 
 
# function to reverse
# bits of a number
def reverseBits(n) :
     
    rev = 0
     
    # traversing bits of 'n' from the right
    while (n > 0) :
         
        # bitwise left shift 'rev' by 1
        rev = rev << 1
         
        # if current bit is '1'
        if (n & 1 == 1) :
            rev = rev ^ 1
         
        # bitwise right shift 'n' by 1
        n = n >> 1
         
     
    # required number
    return rev
     
# Driver code
n = 11
print(reverseBits(n))
 
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# implementation to
// reverse bits of a number
using System;
class GFG
{
    // function to reverse bits of a number
    public static int reverseBits(int n)
    {
        int rev = 0;
 
        // traversing bits of 'n'
        // from the right
        while (n > 0)
        {
            // bitwise left shift
            // 'rev' by 1
            rev <<= 1;
 
            // if current bit is '1'
            if ((int)(n & 1) == 1)
                rev ^= 1;
 
            // bitwise right shift
            //'n' by 1
            n >>= 1;
        }
        // required number
        return rev;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 11;
        Console.WriteLine(reverseBits(n));
    }
}
 
// This code is contributed
// by vt_m.


PHP
 0)
    {
        // bitwise left shift
        // 'rev' by 1
        $rev <<= 1;
         
        // if current bit is '1'
        if ($n & 1 == 1)
            $rev ^= 1;
         
        // bitwise right shift
        // 'n' by 1
        $n >>= 1;
             
    }
     
    // required number
    return $rev;
}
 
// Driver code
$n = 11;
echo reverseBits($n);
 
// This code is contributed by mits
?>


Javascript


输出 :

13

时间复杂度:O(num),其中numn的二进制表示形式中的位数。