Rotar bits de un número

Rotación de bits: una rotación (o cambio circular) es una operación similar al cambio, excepto que los bits que se caen en un extremo se vuelven a colocar en el otro extremo. 
En la rotación a la izquierda, las brocas que se caen en el extremo izquierdo se vuelven a colocar en el extremo derecho. 
En la rotación a la derecha, las brocas que se caen en el extremo derecho se vuelven a colocar en el extremo izquierdo.
 

Ejemplo: 
Let n se almacena usando 8 bits. La rotación a la izquierda de n = 11100101 por 3 hace que n = 00101111 (se desplaza a la izquierda por 3 y los primeros 3 bits se vuelven a colocar en último lugar). Si n se almacena utilizando 16 bits o 32 bits, la rotación a la izquierda de n (000…11100101) se convierte en 00..00 11100101 000.  La
rotación a la derecha de n = 11100101 por 3 hace que n = 10111100 (desplazado a la derecha por 3 y los últimos 3 bits son volver a poner primero) si n se almacena usando 8 bits. Si n se almacena utilizando 16 bits o 32 bits, la rotación a la derecha de n (000…11100101) por 3 se convierte en 101 000..00 11100
 

C++

// C++ code to rotate bits
// of number
#include<iostream>
 
using namespace std;
#define INT_BITS 32
class gfg
{
     
/*Function to left rotate n by d bits*/
public:
int leftRotate(int n, unsigned int d)
{
     
    /* In n<<d, last d bits are 0. To
     put first 3 bits of n at
    last, do bitwise or of n<<d
    with n >>(INT_BITS - d) */
    return (n << d)|(n >> (INT_BITS - d));
}
 
/*Function to right rotate n by d bits*/
int rightRotate(int n, unsigned int d)
{
    /* In n>>d, first d bits are 0.
    To put last 3 bits of at
    first, do bitwise or of n>>d
    with n <<(INT_BITS - d) */
    return (n >> d)|(n << (INT_BITS - d));
}
};
 
/* Driver code*/
int main()
{
    gfg g;
    int n = 16;
    int d = 2;
    cout << "Left Rotation of " << n <<
            " by " << d << " is ";
    cout << g.leftRotate(n, d);
    cout << "\nRight Rotation of " << n <<
            " by " << d << " is ";
    cout << g.rightRotate(n, d);
    getchar();
}
 
// This code is contributed by SoM15242

C

#include<stdio.h>
#define INT_BITS 32
 
/*Function to left rotate n by d bits*/
int leftRotate(int n, unsigned int d)
{
   /* In n<<d, last d bits are 0. To put first 3 bits of n at
     last, do bitwise or of n<<d with n >>(INT_BITS - d) */
   return (n << d)|(n >> (INT_BITS - d));
}
 
/*Function to right rotate n by d bits*/
int rightRotate(int n, unsigned int d)
{
   /* In n>>d, first d bits are 0. To put last 3 bits of at
     first, do bitwise or of n>>d with n <<(INT_BITS - d) */
   return (n >> d)|(n << (INT_BITS - d));
}
 
/* Driver program to test above functions */
int main()
{
  int n = 16;
  int d = 2;
  printf("Left Rotation of %d by %d is ", n, d);
  printf("%d", leftRotate(n, d));
  printf("\nRight Rotation of %d by %d is ", n, d);
  printf("%d", rightRotate(n, d));
  getchar();
}

Java

// Java code to rotate bits
// of number
class GFG
{
static final int INT_BITS = 32;
 
/*Function to left rotate n by d bits*/
static int leftRotate(int n, int d) {
     
    /* In n<<d, last d bits are 0.
       To put first 3 bits of n at
       last, do bitwise or of n<<d with
       n >>(INT_BITS - d) */
    return (n << d) | (n >> (INT_BITS - d));
}
 
/*Function to right rotate n by d bits*/
static int rightRotate(int n, int d) {
     
    /* In n>>d, first d bits are 0.
       To put last 3 bits of at
       first, do bitwise or of n>>d
       with n <<(INT_BITS - d) */
    return (n >> d) | (n << (INT_BITS - d));
}
 
// Driver code
public static void main(String arg[])
{
    int n = 16;
    int d = 2;
    System.out.print("Left Rotation of " + n +
                          " by " + d + " is ");
    System.out.print(leftRotate(n, d));
     
    System.out.print("\nRight Rotation of " + n +
                             " by " + d + " is ");
    System.out.print(rightRotate(n, d));
}
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python3 code to
# rotate bits of number
 
INT_BITS = 32
 
# Function to left
# rotate n by d bits
def leftRotate(n, d):
 
    # In n<<d, last d bits are 0.
    # To put first 3 bits of n at
    # last, do bitwise or of n<<d
    # with n >>(INT_BITS - d)
    return (n << d)|(n >> (INT_BITS - d))
 
# Function to right
# rotate n by d bits
def rightRotate(n, d):
 
    # In n>>d, first d bits are 0.
    # To put last 3 bits of at
    # first, do bitwise or of n>>d
    # with n <<(INT_BITS - d)
    return (n >> d)|(n << (INT_BITS - d)) & 0xFFFFFFFF
 
# Driver program to
# test above functions
n = 16
d = 2
 
print("Left Rotation of",n,"by"
      ,d,"is",end=" ")
print(leftRotate(n, d))
 
print("Right Rotation of",n,"by"
     ,d,"is",end=" ")
print(rightRotate(n, d))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to rotate
// bits of a number
using System;
 
class GFG
{
    static int INT_BITS = 32;
 
    /* Function to left rotate n by d bits*/
    static int leftRotate(int n, int d) {
         
        /* In n<<d, last d bits are 0.
        To put first 3 bits of n at
        last, do bitwise or of n<<d with
        n >>(INT_BITS - d) */
        return (n << d) | (n >> (INT_BITS - d));
    }
     
    /*Function to right rotate n by d bits*/
    static int rightRotate(int n, int d) {
         
        /* In n>>d, first d bits are 0.
        To put last 3 bits of at
        first, do bitwise or of n>>d
        with n <<(INT_BITS - d) */
        return (n >> d) | (n << (INT_BITS - d));
    }
     
    // Driver code
    public static void Main()
    {
        int n = 16;
        int d = 2;
         
        Console.Write("Left Rotation of " + n
                      + " by " + d + " is ");
        Console.Write(leftRotate(n, d));
         
        Console.Write("\nRight Rotation of " + n
                       + " by " + d + " is ");
        Console.Write(rightRotate(n, d));
    }
}
 
// This code is contributed by Sam007

Javascript

<script>
 
// JavaScript code to rotate bits
// of number
let INT_BITS = 32;
 
     
/*Function to left rotate n by d bits*/
 
function leftRotate( n,  d)
{   
    /* In n<<d, last d bits are 0. To
     put first 3 bits of n at
    last, do bitwise or of n<<d
    with n >>(INT_BITS - d) */
    return (n << d)|(n >> (INT_BITS - d));
}
 
/*Function to right rotate n by d bits*/
function rightRotate( n, d)
{
    /* In n>>d, first d bits are 0.
    To put last 3 bits of at
    first, do bitwise or of n>>d
    with n <<(INT_BITS - d) */
    return (n >> d)|(n << (INT_BITS - d));
}
 
 
/* Driver code*/
let n = 16;
let d = 2;
document.write("Left Rotation of " + n +
            " by " + d + " is ");
document.write(leftRotate(n, d));
document.write("<br>");           
document.write("\nRight Rotation of " + n +
            " by " + d + " is ");
document.write(rightRotate(n, d));
 
</script>

C++

#include <bits/stdc++.h>
 
using namespace std;
 
#define SHORT_SIZE 16
 
// function to rotate the given unsigned short
// in the left direction
unsigned short leftRotate(unsigned short x, short d)
{
    /**
     * By doing x << d, we move the first(right most) d bits
     * to the left most d bits, and at the same time we move
     * the left most d bits to the right side,
     * performing OR operation between the two gives use the
     * required result.
     * */
 
    return (x << d) | (x >> (SHORT_SIZE - d));
}
 
// function to rotate the given unsigned short
// in the right direction
unsigned short rightRotate(unsigned short x, short d)
{
    /**
     * By doing x >> d, we move the first(left most) d bits
     * to the right most d bits, and at the same time we move
     * the right most d bits to the right side,
     * performing OR operation between the two gives use the
     * required result.
     * */
 
    return (x >> d) | (x << (SHORT_SIZE - d));
}
 
/* Driver program to test above functions */
int main()
{
    // Test case
    unsigned short n = 28;
    short d = 2;
 
    cout << leftRotate(n, d) << endl;
    cout << rightRotate(n, d) << endl;
 
    return 0;
}
 
// This code is contributed by ganesh227

C

#include <stdio.h>
#define SHORT_SIZE 16
 
// function to rotate the given unsigned short
// in the left direction
unsigned short leftRotate(unsigned short x, short d)
{
    /**
     * By doing x << d, we move the first(right most) d bits
     * to the left most d bits, and at the same time we move
     * the left most d bits to the right side,
     * performing OR operation between the two gives use the
     * required result.
     * */
 
    return (x << d) | (x >> (SHORT_SIZE - d));
}
 
// function to rotate the given unsigned short
// in the right direction
unsigned short rightRotate(unsigned short x, short d)
{
    /**
     * By doing x >> d, we move the first(left most) d bits
     * to the right most d bits, and at the same time we move
     * the right most d bits to the right side,
     * performing OR operation between the two gives use the
     * required result.
     * */
 
    return (x >> d) | (x << (SHORT_SIZE - d));
}
 
/* Driver program to test above functions */
int main()
{
    // Test case
    unsigned short n = 28;
    short d = 2;
    printf("%d\n", leftRotate(n, d));
    printf("%d\n", rightRotate(n, d));
 
    return 0;
}
 
// This code is contributed by ganesh227

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
        int N=28;
          int D=2;
          rotate(N,D);
    }
   
   static void rotate(int N, int D)
    {
        // your code here
        int t=16;
        int left= ((N<<D) | N>>(t-D)) & 0xFFFF;
        int right=((N>>D) | N<<(t-D)) & 0xFFFF;
        System.out.println(left);
         System.out.println(right);
    }
   
}

Python3

SHORT_SIZE = 16
 
# function to rotate the given unsigned short
# in the left direction
def leftRotate(x, d):
 
    return (x << d) | (x >> (SHORT_SIZE - d))
 
  # function to rotate the given unsigned short
# in the right direction
def rightRotate(x, d):
 
    return (x >> d) | (x << (SHORT_SIZE - d)) & 0xDDDDDF
 
# Driver program to test above functions
# Test case
n = 28
d = 2
 
print("Left Rotation of",n,"by"
      ,d,"is",end=" ")
print(leftRotate(n, d))
 
print("Right Rotation of",n,"by"
     ,d,"is",end=" ")
print(rightRotate(n, d))
 
# This code is contributed by shivanisinghss2110

C#

/*package whatever //do not write package name here */
using System;
using System.Collections.Generic;
public class GFG {
    public static void Main(String[] args) {
        int N = 28;
          int D = 2;
          rotate(N, D);
    }
   
  // Driver code
   static void rotate(int N, int D)
    {
        int t = 16;
        int left = ((N<<D) | N>>(t-D)) & 0xFFFF;
        int right = ((N>>D) | N<<(t-D)) & 0xFFFF;
        Console.WriteLine(left);
         Console.WriteLine(right);
    }
   
}
 
// This code is contributed by umadevi9616

Javascript

<script>
 
function rotate(N,D)
{
    // your code here
        let t = 16;
        let left = ((N<<D) | N>>(t-D)) & 0xFFFF;
        let right = ((N>>D) | N<<(t-D)) & 0xFFFF;
        document.write(left + "<br>");
         document.write(right + "<br>");
}
 
let N = 28;
let D = 2;
rotate(N, D);
 
// This code is contributed by avanitrachhadiya2155
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 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 *