C/C ++ & amp; and & amp; | and |,

Source: Internet
Author: User

C/C ++ & | and |,

&&

Indicates the meaning of logic and, that is, and. If the expression on both sides of the operator returns true, the entire operation result is true. Otherwise, the result is false if either of them is false.

For example, the result of 12 & 23 is 1,123 and the result of 1 &-1 is. The result of 0 is 0.

 

& It also has the short circuit function, that is, if the first expression is false, the second expression is no longer calculated. For example, if (str! = Null &&! Str. equals ("") expression. When str is null, the following expression will not be executed, so NullPointerException will not occur.

 

&

It indicates bitwise AND.

& Indicates bitwise AND operation. We usually use 0x0f to perform an & operation with an integer to obtain the minimum four bits of this integer. For example, the result of 0x31 & 0x0f is 0x01.

Binary and operation rules: 1 & 1 = 1 1 & 0 = 0 0 & 0 = 0

Why is 15 and 127 equal to 15?

15 binary: (0000 1111)

127 binary: (1111 1111)

Bitwise AND nature are (0000 1111) = 15

 

|

Indicates logic or

Logic or is a logical operator with the symbol "|" ("or" in PASCAL "). "Logical or" is equivalent to "or" in life. When either of the two conditions is met, the "logical or" operation result is "true"

12 | 1 = 1 12 | 0 = 1 0 | 0 = 0

 

|

Indicates bitwise OR

Bitwise OR operator "|" is a binary operator. The binary (that is, the last digit) corresponding to the two numbers involved in the operation is the same or. If one of the two binary numbers is 1, The result bit is 1.

128: (0001 0000 0000)

127: (0000 1111 1111) (0 for high positions)

Bitwise OR (0001 1111 1111) = 255

Contact Us

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.