First, we need to know that all bitwise operations in C Language refer to bitwise operations of binary numbers. Even if the input is a decimal number, it is stored in binary format in the memory. "<" Usage: The format is: A <m, A, and m must be integer expressions, requiring m> = 0. Function: moves integer a to the left by binary. After the value is removed from the upper position, the value 0 is added to the lower position. ">" Usage: The format is: A> M, A, and m must be integer expressions, requiring m> = 0. Function: Shifts integer a to the right in binary order. After the value is removed from the low position, the value 0 is added.
The shift operation in C language does not have much content. However, if you do not pay attention to it, you will be negligent.
Let's talk less, and 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.
ProgramDuring 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 gets 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. Change
00000110, so the result is 6; x> 1 shifts one digit to the right. Because it is an unsigned number, 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. Change
00000110, so the result is 6; x> 1 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.
In this regard, the C standard does not explicitly specify whether to use logical right shift or arithmetic right shift. However, most machines use the arithmetic shift right
00000001, so the result is still 1. But please note that this is only true for most machines.
Isn't it special?
(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. Change
11111010. 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. The number of unsigned values during the right shift is shift and zero-padding, which is called logical right shift;
In most cases, the number of symbols is the leftmost bit of the shift and population (that is, the maximum valid bit of the population). When the number of digits is shifted, it is called the right shift of arithmetic.
With bytes encoded in the printed memoryCode:
Void print_char (char X)
{
Unsigned char * BP = (unsigned char *) & X;
Int size = sizeof (X );
For (INT I = 0; I <size; I ++)
Printf ("%. 2x", BP [I]);
Printf ("/N ");
}
You can practice it yourself.
reference from: http://blog.chinaunix.net/u1/33888/showart_334911.html