# Bitwise AND (&) bitwise OR (|) bitwise XOR or (^) bitwise inversion (~) Shift left ()

Source: Internet
Author: User

Basic knowledge:

1. And (&) Operations (bitwise AND)
The and operation is usually used for bitwise operations. For example, the result of a number and 1 is the last bit of the binary. This can be used to judge the parity of an integer. the last bit of binary is 0, which indicates that the number is an even number, and the last bit is 1, which indicates that the number is an odd number.
If two digits with the same digit are 1, the value is 1. If one digit is not 1, the value is 0.
00111
11100
(& Or and)
----------------
00100
2. Or (|) (bitwise OR)
The or operation is usually used to assign values unconditionally on Binary location. For example, the result of a number or 1 is to forcibly change the last binary to 1. If you need to change the last bit of the binary to 0, you can subtract one after the number or 1. The actual meaning is to forcibly change the number to the nearest even number.
If one bit is 1, it is 1.
00111
11100
(| Or)
----------------
11111
3. XOR (^) operation (bitwise XOR)
The XOR symbol is XOR.
The XOR operation is usually used to perform the inverse operation on a specific binary bit, because the difference or can be defined as follows: 0 and 1 are not the same or 0 is the same, or 1 is the opposite.
The inverse operation of XOR is itself. That is to say, the final result of two different or the same number remains unchanged, that is, (a xor B) xor B =. The XOR operation can be used for simple encryption. For example, if I want to say 1314520 to my mm, but I am afraid that others will know it, then both parties agree to use my birthday 19880516 as the key. 1314520 XOR 19880516 = 20665500, I will tell mm 20665500. MM calculates 20665500 again
The value of XOR 19880516 gets 1314520, so she understands my attempt.
If the same bit is different, it is 1. If the same bit is different, it is 0.
00111
11100
(^ Or XOR)
----------------
11011

4 .~ If the value is 00111, the result is 11000 (bitwise inversion)

~ It means bitwise inversion.
The binary representation of 57 is (1 byte): 00111001
After bitwise inversion (~ 57) binary: 11000110 indicates decimal:-70
This is a negative number, indicating a signed number. The negative number must be indicated by its complement code in the computer. The complement code is equal to or greater than 1 after the bitwise result is obtained.
Therefore, after the-70 (11000110) symbol bit is reversed by bit, the value is (10111001) plus 1, and the value is (10111010)
To-58 in decimal format.
Therefore ~ 57 =-58

5.> right shift operation
Shifts one digit to the right by dividing by 2; and returns an integer.
M_height> 4 is equivalent:
M_height/16

<Left shift operation
If one digit is left shifted, the value is a * 2.

Today, we see this expression, A | = B, which means a = (a | B), which is a bit similar to a + = B. Example:

`int`  `a = ` `1` `,b = ` `2` `,c = ` `4` `;` `//0x0001,0x0010,0x0100` `a |= b;` `// a = 0x0011 = 3` `b |= c;` `// b = 0x0110 = 6`
`This expression is very useful. This method is applicable in many situations where one value is needed to identify multiple States. SeeHttp://bbs.csdn.net/topics/380232699`

References: http://topic.csdn.net/u/20120312/16/cfc2a3d3-c493-4a60-b3c5-7929df5d0212.html

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.