位旋转:旋转(或循环移位)是与移位类似的操作,不同之处在于,一端掉落的位放回另一端。
在向左旋转时,从左端掉落的钻头会放回到右端。
在向右旋转时,从右端掉落的钻头会放回到左端。
例子:
设n使用8位存储。 n = 11100101向左旋转3会使n = 00101111(向左移位3,并且将前3位放回last)。如果使用16位或32位存储n,则n的左旋转(000…11100101)变为00..00 11100101 000。
如果n使用8位存储,则n = 11100101向右旋转3会使n = 10111100(向右移3,最后3位放回第一位)。如果使用16位或32位存储n,则将n(000…11100101)右旋转3变为101 000..00 11100 。
C++
// C++ code to rotate bits
// of number
#include
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<>(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
#define INT_BITS 32
/*Function to left rotate n by d bits*/
int leftRotate(int n, unsigned int d)
{
/* In 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<>(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<>(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<>(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
输出 :
Left Rotation of 16 by 2 is 64
Right Rotation of 16 by 2 is 4