Bit operations are more easily mixed:The bitwise operation of C and or non-XOR or and arithmetic:&
Both are 1 for 1, otherwise 0
1&1=1, 1&0=0, 0&1=0, 0&0=0
or operation:|
both are 0 for 0, otherwise 1
1|1 = 1, 1|0 = 1, 0|1 = 1, 0|0 = 0
non-arithmetic:~
1 Take 0, 0 take 1
to = 0, ~0 = 1
~ (10001) = 01110
xor operation:^ The
two are equal to 0, unequal to 1 (easy to confuse )
1^1=0, 1
^
0=1, 0
^
1=1, 0^0=0
displacement operator:<< and >>
A bitwise operation is a binary-based operation. In a program, it is often necessary to deal with bits problems. The C + + language provides 6 bit manipulation operators. These operators can only be used for integer operands, that is, only for signed or unsigned char,short,int and long types.
In practical applications, it is recommended to use the unsigned integer operand, because the signed operand may be different depending on the machine result.
The bitwise operator operators for C + + and C are identical, but the Bitset standard library is added in C + + to support bit set operations, please refer to http://book.51cto.com/art/200809/88698.htm or http:// Book.csdn.net/bookfiles/17/1001760.shtml, these links are "C + + Primer Chinese Version" of the content, you can also read directly.
The following is a list of C/s + + bit manipulation operators, where operator precedence is descending from top to bottom, but the <<,>> priority is the same.
A/C + + bit manipulation operator
Operator |
Function |
Usage |
~ |
Bit negation |
~expr |
<< |
Move left |
Expr1 << EXPR2 |
>> |
Move right |
Expr1 >> EXPR2 |
& |
Bit and |
Expr1 & EXPR2 |
^ |
Bit XOR or |
Expr1 ^ expr2 |
| |
Bit or |
Expr1 | Expr2 |
The code examples are as follows:
[CPP]View Plaincopyprint?
- #include <iostream>
- using namespace std;
- int Main () {
- unsigned short x=3,y=5;
- cout<<"~x="<< (unsigned short) ~x<<endl; //bit negation
- cout<<"~x="<<~x<<endl; //bit negation
- cout<<"x&y="<< (x&y) <<endl; //bit and
- cout<<"x^y="<< (x^y) <<endl; //Bit XOR or
- cout<<"x|y="<< (x|y) <<endl; //bit or
- cout<<"x<<1="<< (x<<1) <<endl; //bit left shift
- cout<<"y>>1="<< (y>>1) <<endl; //bit right shift
- return 0;
- }
<textarea name="code" class="cpp" style="display: none;" rows="15" cols="50">#include <iostream>using namespace Std;int main () {unsigned short x=3,y=5;cout<< "~x=" << ( unsigned short) ~x<<endl;//-cout<< "~x=" <<~x<<endl;//-bit negation cout<< "x&y=" << ( X&y) <<endl;//bit with cout<< "x^y=" << (x^y) <<endl;//bit xor cout<< "x|y=" << (X|y) < <endl;//bit or cout<< "x<<1=" << (x<<1) <<endl;//bit left shift cout<< "y>>1=" << ( y>>1) <<endl;//bit right shift return 0;}</textarea>
The results of the operation are as follows:
~x=65532
~x=-4
X&y=1
X^y=6
X|y=7
X<<1=6
y>>1=2
The code is interpreted as follows:
Short is a 16-bit integer, so the binary representation of x, Y is as follows:
X=3 (00000000 00000011)
Y=5 (00000000 00000101)
~ 00000000 00000011
= 11111111 11111100 (65532 or-4) (as to why the same bits represents a different number, this is related to the numerical representation of the computer, for specific reasons to search for "complement")
00000000 00000011
& 00000000 00000101
= 00000000 00000001 (1)
00000000 00000011
^ 00000000 00000101
= 00000000 00000110 (6)
00000000 00000011
| 00000000 00000101
= 00000000 00000111 (7)
00000000 00000011<<1
= 00000000 00000110 (6)
00000000 00000101>>1
= 00000000 00000010 (2)
Example reference: bitwise operator (i): c/C + + bitwise operator-Jason's Column-Blog channel-csdn.net
http://blog.csdn.net/jason314/article/details/5719933
The bitwise operation of C and or non-XOR or