The shift operation in C language does not have much content. However, if you do not pay attention to it, you will be negligent.
Make two questions first.
(1) unsigned char x = 3;
What is x <1? X> what is 1?
(2) char x = 3;
What is x <1? X> what is 1?
(3) char x =-3;
What is x <1? X> what is 1?
3. the binary number is 00000011;-3 The binary number is 11111101.
During program execution, the operation is the numerical encoding representation, that is, the binary representation of the numerical value in the memory. For example, when the program gets-3, it will get 11111101.
(1) For the unsigned number 3, x <1 shifts to the left, the leftmost displacement is lost, and the rightmost shifted bits are filled with zeros. To 00000110, so the result is 6; x> 1 shifts one digit to the right. Because it is a number of unsigned values, the logic shifts right and the rightmost One shifts away, the number of bits moved to the leftmost is 0, which is 00000001. Therefore, the result is 1.
(2) For the signed number 3, x <1 shifts to the left, the leftmost displacement is lost, and the rightmost shifted bits are filled with zeros. To 00000110, so the result is 6; x> 1 shifts one digit to the right. Because of the number of symbols, the logical right shift may occur, or the arithmetic right shift may occur, the C standard does not explicitly specify whether to use logical right shift or arithmetic right shift. However, most machines use arithmetic shifts right to 00000001, so the result is still 1. But please note that most machines are like this. Do you want to ensure that you will not encounter any special circumstances?
(3) For the signed number-3, x <1 shifts to the left, the leftmost displacement is dropped, and the rightmost shifted bit is set to zero. The result is-6. Shifts one digit to the right. Because it is a signed number, the logical right shift may occur, or the arithmetic right shift may occur. Most machines use the arithmetic shift right to 11111110 and the result is-2.
Conclusion: The left shift always shifts and zeros are filled. When the right shift is performed, the unsigned number is the shift and zero-padding, which is called the logical right shift. In most cases, the signed number is the shift and the leftmost bit of the population (that is, the maximum valid bit of the population ), it is called the right shift of arithmetic.
The code for printing the byte encoding in the memory is provided:
1. void print_char (char x)
2 .{
3. unsigned char * bp = (unsigned char *) & x;
4. int size = sizeof (x );
5. for (int I = 0; I <size; I ++)
6. printf ("%. 2x", bp [I]);
7. printf ("\ n ");
8 .}
1. # include <stdio. h>
2.
3. int main (void)
4 .{
5. char a = 195; // The binary value is 1100 0011.
6. char B = 0;
7. B = (a <2); // "<" Symbol: shifts left, and the right-side blank bit is 0.
8. printf ("% d \ n", B); // the result after the shift is 0000 1100, And the decimal number is 12.
9. return 0;
10 .}
Realize cyclic shift in C Language
The C language does not provide the cyclic shift operator, but it can be implemented in a concise way. This is a very simple macro that can implement cyclic shift in C. You can also refer to other languages.
1. # define ROTATE_LEFT (x, n) (x) <(n) | (x)> (8 * sizeof (x)-(n )))
2. # define ROTATE_RIGHT (x, n) (x)> (n) | (x) <(8 * sizeof (x)-(n )))
The shift symbol in C language: "<" and ">", cannot realize cyclic shift. You can refer to the following:
For example, move the = 0x45 loop to the left.
A loop shifts n places to the left, that is, the original right side (8-n) shifted to n places, and the original left side shifted to n places on the rightmost side.
1. Place the n-bit left of a in the n-bit height of B.
B => (8-N );
2. Shift a n places left, and the n places on the right side of a are supplemented with 0
C = <n;
3. Perform or operations on B and c.
A = c | B;
The procedure is as follows:
1. main ()
2 .{
3. unsigned char a = 0x45, B, c;
4. unsigned int n = 2;
5. B = a> (8-N)
6. c = a <n;
7. a = c | B;
8 .}
This article is from the "liangbing" blog