Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end.
In the left rotation, the bits that fall off at the left end are put back at the right end.
In the right rotation, the bits that fall off at the right end are put back at the left end.
1 Introduction
循环位移:rotate the bits。
Bit rotation, also known as bit shifting, is a technique for rotating the bits of a binary number to the left or right. This can be useful in a variety of contexts, such as in computer science and cryptography.
2 Code
2.1 python code
# 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
2.1.1 Output
Left Rotation of 16 by 2 is 64
Right Rotation of 16 by 2 is 4
2.1.2 复杂度分析
Time Complexity: O(1)
Auxiliary Space: O(1)
2.2 cpp code
/**
* 自定义的一个加密类,对message中的每一个byte进行循环右移处理。解密的时候,就是对message中的每一个byte进行循环左移处理。
*/
#include <stdio.h>
#include <assert.h>
#define LENGTH(array) (sizeof(array) / sizeof(array[0]))
unsigned char INT_BITS = 8;
unsigned char MAX_VALUE = 0xFF;
// Function to left
// rotate n by d bits
unsigned char leftRotate(unsigned char n, unsigned char 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) & MAX_VALUE) | (n >> (INT_BITS - d));
}
// Function to right
// rotate n by d bits
unsigned char rightRotate(unsigned char n, unsigned char 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)) & MAX_VALUE);
}
unsigned char* encrypt(unsigned char key[], int keyLen, unsigned char message[], int msgLen) {
assert(key != NULL && keyLen == 1);
unsigned char shift_count = key[0];
assert(shift_count > 0 && shift_count < INT_BITS);
unsigned char* output = new unsigned char[msgLen];
for (int i = 0; i < msgLen; i++) {
output[i] = rightRotate(message[i], shift_count);
}
return output;
}
unsigned char* decrypt(unsigned char key[], int keyLen, unsigned char encryptMessage[], int msgLen) {
assert(key != NULL && keyLen == 1);
unsigned char shift_count = key[0];
assert(shift_count > 0 && shift_count < INT_BITS);
unsigned char* output = new unsigned char[msgLen];
for (int i = 0; i < msgLen; i++) {
output[i] = leftRotate(encryptMessage[i], shift_count);
}
return output;
}
void test_case1() {
unsigned char data[] = {16};
unsigned char key[] = {2};
unsigned char* encrypt_message = encrypt(key, 1, data, 1);
printf("%d\n", encrypt_message[0]); // 4
unsigned char* plain = decrypt(key, 1, encrypt_message, LENGTH(encrypt_message));
printf("%d\n", plain[0]); // 16
}
int main(int argc, char *argv[])
{
test_case1();
}
参考资料
[1] https://www.geeksforgeeks.org/python3-program-to-rotate-bits-of-a-number/
[2] https://www.geeksforgeeks.org/rotate-bits-of-an-integer/