When we talk about the development of computers, we will certainly refer to binary system. Although the importance of binary to computers is often emphasized, the use of high-level languages (such as C #) is still relatively small in development. But this relatively few uses, and does not become we do not understand his excuse.
One or two binary operations.
(i) Arithmetic operations
Understood from the familiar decimal in our daily life. Reduced from decimal "0,1,2,3,4,5,6,7,8,9" to "0,1". The capacity of each bit varies from 10 to 2, that's all, that's it.
1. Addition: 0+0=0,0+1=1, 1+0=1, 1+1=10 (High carry)
2. Subtraction: 0-0=0,0-1=1 (to high borrow) 1-0=1,1-1=0
3. Multiplication: 0*0=0,0*1=0,1*0=0,1*1=1
4. Division: 0÷0=0,0÷1=0,1÷0=0 (same as decimal, cannot be removed by 0, meaningless), 1÷1=1
Speaking of this, tell us a cold joke. At noon today, the elevator has reached the 1 floor, but I still froze there. What do you think? Because I'm still waiting for the 0 floor.
This is a joke that programmers can understand.
(ii) Logical operation
1 is interpreted as a Boolean value of True, and 0 is interpreted as a Boolean value of false.
1. Addition: usually denoted by the symbol "+" or "∨" (or operation)
One is true and the result is true.
0+0=0, 0∨0=0
0+1=1, 0∨1=1
1+0=1, 1∨0=1
1+1=1, 1∨1=1
2. Multiplication: Usually with the symbol "X" or "∧" or "•" To represent (and operate)
When all is true, the result is true. 0x0=0, 0∧0=0, 0 0=00x1=0, 0∧1=0, 0 1=01x0=0, 1∧0=0, 1 0=01x1=1, 1∧1=1, 1 1=1 3. Negation: (non-operation) is originally true, the result is false, the original is false, the result is true. 0=1 non-0 is 11=0 non-1 is 0 4. XOR: Usually denoted by the symbol "⊙" (semi-add operation) Only one is true, one is false, the result is true. 0⊕0=0 0 with 0 xor, the result is 00⊕1=1 0 with 1 xor, the result is 11⊕0=1 1 with 0 xor, the result is 11⊕1=0 1 with 1 XOR, the result for the 0 (three) bit operation to understand the logical operation, then look at the bit operation is much easier. To put it simply, a bitwise operation is a logical operation of a binary corresponding bit. The conversion of decimal 5 to binary is 101, and the decimal 19 to binary 10011 for example. 1. Bitwise OR: Usually with the symbol "|" or "or" means that the 101|10011=00101|10011 each corresponds to a logical OR operation, and a party of 1 is 1. The final result is: 10111. 2. Bitwise AND: Usually denoted by the symbol "&" or "and" 00101&10011 you do the logic and the operation, the two sides are 1 results only 1 The final result is: 1 3. Bitwise XOR: Usually denoted by the symbol "^" or "XOR" 00101^ 10011 members according to the logical XOR operation, one side is 0 and the other side is 1 result is 1 The final result is: 10110 4. Bitwise negation: Usually the symbol "~" or "not" means that each of you, 0 for 1, 1 for 0. Take one byte (eight-bit) for example: ~0000 0101=1111 1010~0001 0011=1110 1100 5. Bitwise left: Usually the symbol "<<" or "SHL" means that you move the corresponding number of digits to the left. 0000 0101<<1=0000 10100001 0011<<1=0010 0110 6. Bitwise right SHIFT: usually denoted by the symbol ">>" or "shr" 0000 0101>>1=0000 00100001 0011>>1=0000 1001 Second, application examples we illustrate the use of binary with the enumeration values affixed with the flags tag. First, create an enumeration class.
[Flags] publicenum pfive { 10, 1 1 , 1 2 , 1 3 , 1 4 }
Here, we use the bitwise left SHIFT operation when assigning values with an enumeration. Through the above introduction, we can easily calculate:
Russia = 1 << 0=1 (binary 1)
China = 1 << 1=2 (binary 10)
USA = 1 << 2=4 (binary 100)
UK = 1 << 3=8 (binary 1000)
France = 1 << = 16 (binary 10000)
Put 1 in the binary bit, step by step to the left, is not very intuitive.
We will then create a way to judge which countries in the permanent membership are English-speaking countries.
Public Static stringCanspeakenglish (pfive p5) {if(((Pfive.usa | pfive.uk) & P5) = =p5) { returnP5. ToString () +"Is a 中文版 country."; } Else { returnP5. ToString () +"is not a 中文版 country."; } }
Here we focus on the code of this sentence:
((Pfive.usa | pfive.uk) & P5) = = P5
We know that the United States and Britain are English-speaking countries, so long as the incoming country is one of these two countries, it is the English-speaking country.
pfive.usa=4=100;
pfive.uk=8=1000;
Pfive.usa | pfive.uk=100|1000=1100;
Now assume that the country in which it was introduced is China
pfive.china=2=10;
(Pfive.usa | pfive.uk) &pfive.china=1100|0010=0000
0000!=pfive.china, return False
Now suppose that the country passed in is the UK
pfive.uk=8=1000;
(Pfive.usa | pfive.uk) &pfive.uk=1100|1000=1000
1000==pfive.uk, returns True
So by bitwise operation, a simple judgment can be made as above.
Here's what we need to mention, why can't we use normal enumeration values? Very simply, if the russia=1,china=2,usa=3, then 3 can represent the USA, also can represent Russia and China two countries, this will cause a kind of confusion.
Understanding of binary operation and its application in code