We know that bit operations are widely used in computing. Bit operations are also an essential operation in various programming languages of the computer, especially in the lower-level implementation code of the computer.
Let's take a look at the bit operations.
1. Left shift arithmetic << left shift right shift are all moving binary numbers
0000-0000 0000-0000 0000-0000 0000-1100 = 12 Move one to the left (several 0 on the right)
0000-0000 0000-0000 0000-0000 0001 1000 =24 and move left one
0000-0000 0000-0000 0000-0000 0011 0000 =48
Thus, we can get, in fact, M to the left N-bit, m=m*2^n, that is, each left one bit, the number will increase to one-fold.
2. Right-shift and left-shift operations are similar, but there is a difference.
0000-0000 0000-0000 0000-0000 0000-1100 = 12 Move Right One
0000-0000 0000-0000 0000-0000 0000-0110 =6 and move one to the right.
0000-0000 0000-0000 0000-0000 0000-0011 =3 move one more right
0000-0000 0000-0000 0000-0000 0000-0001 =1
We can also get a rule that moves one bit to the right, and the number decreases by one time (decreasing by the computer's integer law)
>> to the right of the process, the left is a complement 0 or 1, is based on the original number of the highest bit to determine the original number of the highest bit is 1, then 1, or 0
>>> the process of moving to the right, regardless of the original number of the highest bit is 1 or 0, are 0, so we should choose according to the need, the right shift is the use of >>> or >>
3.& and arithmetic
The same is true of bits. such as 14&7=2 (omitting the previous bits)
1010
&0111
----------------
0010
4.| or operations are similar to operations (slightly)
5.~ operations are pretty much the same.
6.^, we look at the XOR operation 12^7=11
1100
^0111
--------------------------
1011
An XOR operation is a two-digit difference of 1, and the same is 0.
7. Let's talk about how the binary is transformed into a 16 binary in the computer.
0000-0000 0000-0000 0000-0000 0010-1011
As we all know, as long as we take every four bits, and then figure out the 16 of each item, and then it's the 2-binary representation of the 16-binary.
The data above should be 2 11
How is the computer counted? This is done with the & operation,
First, the upper number is the following and the operation
0000-0000 0000-0000 0000-0000 0010-1011
&0000-0000 0000-0000 0000-0000 0000-1111
---------------------------------------------------------------------
0000-0000 0000-0000 0000-0000 0000-1011
So, we take out the last four bits of the data above, and then we get the value of 16 in the bits.
If we were to take out the penultimate four-bit bits, how should we take it?
We just need to move the book four bits to the right, and then we'll get it. Very simple, it is also one of the applications of bit arithmetic in computer.
8. Let's talk about how to exchange two numbers with a bitwise operation.
int a=8,b=2;
The most common method: int temp=a,a=b,b=temp, the simplest and most commonly used, not much to say.
A little bit more advanced: a=a+b
B=a-b
A=a-b
More advanced bit arithmetic: a=a^b
B=a^b
A=a^b
That's why.
Because the XOR operation has a characteristic, the following 10^8=2
1010
^1000
------------
0010
But 10^8^8=???? =10
0010
^ 1000
-------------
1010
That is, n ^m^ m=n, Magic Bar, slowly grasp will be.