- C language
- Basic grammar
- here
Shift operator
This section describes the shift operator in C language. A shift operation is an operation that moves the bit position to the right or left.
Although not defined in the C language specification, as a de facto standard, assume that logical shift operations are implemented for unsigned integers and arithmetic shift operations are implemented for signed integers. Both VC ++ and gcc do this.
However, since this is implementation-dependent, there may be some implementations that do not work, but unless implemented in this way, logical shift operations and arithmetic shift operations cannot be defined in C language, and a portable library is available. I can't write it, so as time goes by, I imagine that it will be implemented by the implementation on the de facto standard side.
I will explain based on this premise.
Left logical shift operation
The left logical shift operation logically shifts to the left a specified number of times. x is an unsigned integer.
// Left logical shift operation x << count
Suppose the value is unsigned 8-bit integer. When this is shifted to the left by 2 bits, it becomes as follows. The part exceeding 8 bits is truncated. The right side is filled with 0s.
// If you shift this to the left by 2 bits 10101101 // It looks like this 10101101 // Truncate the part over 8bit and fill the right side with 0 10110100
This is a sample program for left logical shift operation.
#include <stdio.h> #include <stdint.h> int main (void) { // 10101101 uint8_t x = 0xAD; // 2bit left logical shift uint8_t y = x << 2; printf("%X\n", y); }
This is the output result. The binary number is "10110100".
B4
Right logical shift operation
The right logical shift operation logically shifts to the right a specified number of times. x is an unsigned integer.
// Right logical shift operation x << count
Suppose the value is unsigned 8-bit integer. When this is shifted to the right by 2 bits, it becomes as follows. The part below 1bit is truncated. The left side is filled with 0s.
// If you shift this to the right by 2 bits 10101101 // It looks like this 10101101 // Truncate the part below 1bit and fill the left side with 0 00101011
This is a sample program for right logical shift operation.
#include <stdio.h> #include <stdint.h> int main (void) { // 10101101 uint8_t x = 0xAD; // 2bit right logical shift uint8_t y = x >> 2; printf("%X\n", y); }
This is the output result. The binary number is "00101011".
2B
Left arithmetic shift operation
The left arithmetic shift operation shifts the arithmetic to the left a specified number of times. x is a signed integer. Left arithmetic shift is exactly the same operation as left logical shift.
// Left arithmetic shift operation x << count
Suppose the value is signed 8-bit integer. When this is shifted to the left by 2 bits, it becomes as follows. The part exceeding 8 bits is truncated. The right side is filled with 0s.
// If you shift this to the left by 2 bits 10101101 // It looks like this 10101101 // Truncate the part over 8bit and fill the right side with 0 10110100
This is a sample program for left arithmetic shift operation.
#include <stdio.h> #include <stdint.h> int main (void) { // 10101101 int8_t x = 0xAD; // 2bit left arithmetic shift int8_t y = x << 2; // Output unsigned because I want to see what happened to the bits printf("%X\n", (uint8_t) y); }
This is the output result. The binary number is "10110100".
B4
Right arithmetic shift operation
The right arithmetic shift operation shifts the arithmetic to the right a specified number of times. x is a signed integer.
// Right arithmetic shift operation x << count
Suppose the value is signed 8-bit integer. When this is shifted to the right by 2 bits, it becomes as follows. The part below 1bit is truncated. The left side is filled with the sign bit (the leftmost bit, in this case 1).
// If you shift this to the right by 2 bits 10101101 // It looks like this 10101101 // Truncate the part below 1bit and fill the left side with the sign bit (the leftmost bit of the original bit, in this case 1) 11101011
This is a sample program for right arithmetic shift operation.
#include <stdio.h> #include <stdint.h> int main (void) { // 10101101 int8_t x = 0xAD; // 2bit right arithmetic shift int8_t y = x >> 2; // Output unsigned because I want to see what happened to the bits printf("%X\n", (uint8_t) y); }
This is the output result. The binary number is "11101011".
EB
Where do you use shift operation?
Well, I don't usually use it (laughs). If you're building an application, don't you use it?
However, I don't know the edge case, so I'll just say "I don't think I'll use it."
It's unlikely that using shift operations will be faster than arithmetic operations, as the compiler will automatically optimize them for you.
Again, I don't know the edge case, so I'll keep it to the extent that it's unlikely.
When creating a library, use shift operations for functions that generate pseudo-random numbers or generate hash values with MD5 or SHA. You see that it is used in the implementation when you want to express randomness programmatically. I think you will often see it in the functions of the library related to encryption.