Invertir bits reales del número dado

Dado un entero no negativo n . El problema es invertir los bits de n e imprimir el número obtenido después de invertir los bits. Tenga en cuenta que se está considerando la representación binaria real del número para invertir los bits, no se están considerando los 0 iniciales.
Ejemplos: 
 

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.

En este enfoque, uno por uno bit en la representación binaria de n se obtiene con la ayuda de la operación de desplazamiento a la derecha bit a bit y se acumulan en rev con la ayuda de la operación de desplazamiento a la izquierda bit a bit. 
Algoritmo: 
 

C++

// C++ implementation to reverse bits of a number
#include <bits/stdc++.h>
 
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;
}

C

// C implementation to reverse bits of a number
#include <stdio.h>
 
// 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;
    printf("%u",reverseBits(n));
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta (kriSania804)

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

<?php
// PHP implementation to reverse
// bits of a number
 
// function to reverse
// bits of a number
function reverseBits($n)
{
    $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 code
$n = 11;
echo reverseBits($n);
 
// This code is contributed by mits
?>

Javascript

<script>
 
// JavaScript program  to
// reverse bits of a number
 
    // function to reverse bits of a number
    function reverseBits(n)
    {
        let 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 code
 
        let n = 11;
        document.write(reverseBits(n));
 
</script>
Producción

13

Complejidad de tiempo : O(num), donde num es el número de bits en la representación binaria de n .

Complejidad espacial: O(1)
 

Publicación traducida automáticamente

Artículo escrito por ayushjauhari14 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *