In general C Programming, bit operations are not frequently used, so they are often forgotten. the day before yesterday, I did a Data Structure assignment in the school. I had to ask for the power of 2 n. I just moved it to the left and right for review.
First, move left. To move left is to move all the bits of a number to the left. In C, the <operator is used. For example:
Int I = 1;
I = I <2; // shifts the value in I two places to the left.
That is to say, the binary system of 1 is 000... 0001 (here, the number of 0 in front of 1 is related to the number of digits in int, 32-bit machine, 31 0 in GCC), shifted to 000 after 2 digits left... 0100, that is, 4 in decimal system. Therefore, if the Left shift of 1 is equivalent to multiplying by 2, then the left shift of N is multiplied by the Npower of 2 (the number of symbols is not fully applicable, this is because the left shift may cause symbol changes. The reasons are described below)
One problem that needs to be noted is the symbol bit at the leftmost end of the int type and the shift out. we know that Int Is the signed integer number, and the first bit on the leftmost side is the signed bit, that is, 0 is positive and 1 is negative. Then, overflow occurs during shift. For example:
Int I = 0x40000000; // 40000000 of hexadecimal notation, 01000000 of hexadecimal notation... 0000
I = I <1;
Then, after I shifts 1 to the left, it will become 0x80000000, that is, 100000 of the binary system... 0000, the sign bit is set to 1, and the other bits are all 0, which is changed to the minimum value that can be expressed by the int type. The 32-bit int value is-2147483648, overflow. what will happen if I is moved 1 to the left? In C language, the highest bit is discarded. After 1 is discarded, the value of I is changed to 0.
A special case in the left shift is that when the number of digits in the left shift exceeds the maximum number of digits in the value type, the compiler will use the number of digits in the left shift to de-model the maximum number of digits and then shift by the remainder, for example:
Int I = 1, j = 0x80000000; // set int to 32 characters
I = I <33; // 33% 32 = 1 shifted to 1, and I changed to 2
J = j <33; // 33% 32 = 1 shifts 1 bit left, j changes to 0, and the highest bit is discarded.
When compiling this program with gcc, the compiler will provide a warning, indicating the number of places to move left> = type length. in fact, I, j moves the remainder of 1 bit, that is, 33% after 32. this rule is implemented in gcc. It is unclear whether other compilers are the same.
In short, the Left shift is: discard the highest bit, 0 fill the second bit
Now that the right shift is clear, the right shift is better understood.
The concept of right shifting is opposite to that of left shifting. It is to move several digits to the right. The operator is>.
The bitwise of the right-shift symbol is different from that of the left-shift symbol. For signed integers, for example, int type, the right-shift symbol remains unchanged. For example:
Int I = 0x80000000;
I = I> 1; // The I value will not change to 0x40000000, but 0xc0000000
That is to say, after the sign bit moves to the right, if it is positive, it fills in 0, and if it is negative, it fills in 1, that is, the arithmetic shift in assembly language to the right. similarly, when the number of digits to be moved exceeds the length of the type, the remainder is obtained and then the remainder is moved.
In short, in C, the Left shift is the logical/arithmetic left shift (the two are identical), and the right shift is the arithmetic right shift, which will keep the symbol bit unchanged. in practical applications, you can use the left/right shift to perform fast multiplication/Division operations, which is much more efficient than loop operations.
From http://blog.chinaunix.net/u/19186/showart_167761.html
# Include "stdafx. h"
# Include <iostream>
# Include <stdio. h>
Using namespace std;
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int a = 3;
Cout <a <endl;
Char * buf = new char (20 );
Buf = itoa (a, buf, 2 );
Cout <buf <endl;
A <= 2;
Cout <a <endl;
Buf = itoa (a, buf, 2 );
Cout <buf <endl;
A> = 1;
Cout <a <endl;
Buf = itoa (a, buf, 2 );
Cout <buf <endl;
Int I = 1, j = 0x80000000;
Printf ("% d/n", j );
I <= 33;
J <= 33;
Cout <I <":" <j <endl;
Printf ("% d/n", j );
Int k = 0x80000000;
K> = 1;
Buf = ITOA (K, Buf, 2 );
Cout <Buf <Endl;
Printf ("% 0x/N", k );
Return 0;
}