Bit Rotations
Bit rotation, also called a circular shift, is a bitwise operation that moves the bits of a value around its ends. Unlike standard left or right bit shifts—which move bits toward one end and fill the emptied positions with zeros—bit rotation wraps the bits that fall off one end back around to the other end. This means that no information is lost, and the overall pattern of bits is preserved, just rearranged. In C, there are no built-in operators for bit rotation, so you must use a combination of shifts and bitwise OR to achieve this effect.
bit_rotation.c
1234567891011121314151617181920212223#include <stdio.h> #include <stdint.h> // Rotate left for 8-bit values uint8_t rotate_left8(uint8_t value, unsigned int n) { n = n % 8; // Only 0-7 meaningful for 8 bits return (value << n) | (value >> (8 - n)); } // Rotate right for 8-bit values uint8_t rotate_right8(uint8_t value, unsigned int n) { n = n % 8; return (value >> n) | (value << (8 - n)); } int main() { uint8_t val = 0b10110011; // 179 in decimal printf("Original: 0x%02X\n", val); printf("Rotate left by 3: 0x%02X\n", rotate_left8(val, 3)); printf("Rotate right by 2: 0x%02X\n", rotate_right8(val, 2)); return 0; }
Bit rotations are useful in several areas. In cryptography, they help mix bits in algorithms such as block ciphers and hash functions, making it harder to reverse or predict the transformation. Rotations are also used in checksum and error-detection algorithms, where they help spread bit changes throughout a value for better detection of errors. Because rotations do not discard any bits, they are ideal for tasks that require all original information to be preserved, just rearranged.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 6.67
Bit Rotations
Sveip for å vise menyen
Bit rotation, also called a circular shift, is a bitwise operation that moves the bits of a value around its ends. Unlike standard left or right bit shifts—which move bits toward one end and fill the emptied positions with zeros—bit rotation wraps the bits that fall off one end back around to the other end. This means that no information is lost, and the overall pattern of bits is preserved, just rearranged. In C, there are no built-in operators for bit rotation, so you must use a combination of shifts and bitwise OR to achieve this effect.
bit_rotation.c
1234567891011121314151617181920212223#include <stdio.h> #include <stdint.h> // Rotate left for 8-bit values uint8_t rotate_left8(uint8_t value, unsigned int n) { n = n % 8; // Only 0-7 meaningful for 8 bits return (value << n) | (value >> (8 - n)); } // Rotate right for 8-bit values uint8_t rotate_right8(uint8_t value, unsigned int n) { n = n % 8; return (value >> n) | (value << (8 - n)); } int main() { uint8_t val = 0b10110011; // 179 in decimal printf("Original: 0x%02X\n", val); printf("Rotate left by 3: 0x%02X\n", rotate_left8(val, 3)); printf("Rotate right by 2: 0x%02X\n", rotate_right8(val, 2)); return 0; }
Bit rotations are useful in several areas. In cryptography, they help mix bits in algorithms such as block ciphers and hash functions, making it harder to reverse or predict the transformation. Rotations are also used in checksum and error-detection algorithms, where they help spread bit changes throughout a value for better detection of errors. Because rotations do not discard any bits, they are ideal for tasks that require all original information to be preserved, just rearranged.
Takk for tilbakemeldingene dine!