First, we need to understand that all the bit operations in the C language refer to the bitwise operations of the binary numbers. Even if you enter a decimal number, it is stored as a binary form in memory. "<<" Method of Use: The format is: A<<m,a and M must be an integral expression, requires m>=0. Function: The integer number a press bits to the left to move the M-bit, after the high position removed, the low 0. ">>" Method of Use: The format is: A>>m,a and M must be an integral expression, requires m>=0. Function: The integer number a press bits to the right to move the M bit, after the low shift out, high 0
The shift operation in C language is not much content. Just some places you don't pay attention to, you neglect.
Gossip less say, first do two small questions first.
(1) unsigned char x=3;
How much is x<<1? How much is x>>1?
(2) Char x=3;
How much is x<<1? How much is x>>1?
(3) Char x=-3;
How much is x<<1? How much is x>>1?
3 written binary number is 00000011;-3 written in binary number is (complement) 11111101.
When the program runs, it is the encoded representation of the numeric value, which is the binary representation of the value in memory. Say
When the program takes 3, it takes 11111101.
(1) To unsigned number three said, X<<1 to the left one, the leftmost displacement is dropped, the rightmost move in the bit to fill 0. Become
00000110, so the result is a 6;x>>1 to the right, because it is unsigned number, so the logical right shift, the rightmost one moved off,
The leftmost bit is 0 and becomes 00000001, so the result is 1.
(2) for the signed number of three said, X<<1 to the left one, the leftmost displacement dropped, the rightmost move in the bit to fill 0. Become
00000110, so the result is a 6;x>>1 to the right, because it is a signed number, may occur a logical right shift, may also occur arithmetic right shift
, the C standard does not clearly specify whether to use logical right SHIFT or arithmetic right shift. But most machines use arithmetic to move right, to become
00000001, so the result is still 1. But please note that this is just saying that most of the machines are this, you dare to guarantee yourself
No special circumstances?
(3) for the signed number-three said, X<<1 to the left one, the leftmost displacement is dropped, the rightmost move in the bit to fill 0. Become
11111010, the result is-6. Move right one bit, because it is a signed number, the logical right shift may occur, and the arithmetic right shift can occur.
Most machines use arithmetic right to shift to 11111110, which turns out to be-2.
Summary: Always shift and fill 0 when moving left. When moving right, the unsigned number is shift and complement 0, which is called logical right shift.
The number of symbols in most cases is the shift and fill the leftmost bit (that is, the most significant bit), move a few to fill several, this is called arithmetic right shift.
Code with byte encoding in print memory:
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");
}
Be able to practice your own practice.
Referenced from: http://blog.chinaunix.net/u1/33888/showart_334911.html
C Language Shift operation