Boolean logical operators

Source: Internet
Author: User
Boolean logical operators-general Linux technology-Linux programming and kernel information. For more information, see the following section. The number of boolean operators can only be boolean. The logical operation results are also Boolean (see Table 4-5 ).

Table 4-5 Boolean logical operators and their meanings

Operator meaning
& Logic and
| Logic or
^ Exclusive or
| Short circuit or
& Short circuit and
! Logical Inversion
& = Logic and value assignment (abbreviated form of value assignment)
| = Logic or value assignment (abbreviated form of value assignment)
^ = XOR assignment (abbreviated form of assignment)
= Equal
! = Not equal
? : Ternary operator (IF-THEN-ELSE)

Boolean operators "&", "|", and "^" perform the same operation on the Boolean value as they perform on the integer. Logical operator "!" The result indicates the opposite state of the Boolean value :! True = false and! False = true. The calculation results of each logical operator are shown in Table 4-6:
The number of boolean operators can only be boolean. The logical operation results are also Boolean (see Table 4-5 ).

Table 4-5 Boolean logical operators and their meanings

Operator meaning
& Logic and
| Logic or
^ Exclusive or
| Short circuit or
& Short circuit and
! Logical Inversion
& = Logic and value assignment (abbreviated form of value assignment)
| = Logic or value assignment (abbreviated form of value assignment)
^ = XOR assignment (abbreviated form of assignment)
= Equal
! = Not equal
? : Ternary operator (IF-THEN-ELSE)

Boolean operators "&", "|", and "^" perform the same operation on the Boolean value as they perform on the integer. Logical operator "!" The result indicates the opposite state of the Boolean value :! True = false and! False = true. The calculation results of each logical operator are shown in Table 4-6:
The number of edge operations depends on whether the number on the left is true or false. The short-circuit version is useful. For example, the following program statements describe the advantages of short-circuit logical operators and use them to prevent the error of division by 0:

If (denom! = 0 & num/denom> 10)

Since the short-circuit AND operator is used, there will be no unexpected running errors when the denom is 0. If this line of code uses the standard AND operator (&), it will evaluate both computations. When there is a division of 0, it will generate a runtime error.

Since short-circuit operators are valid in Boolean logic operations, they are used in Boolean logic operations, while standard and or operators (only one character) are used only in bitwise operations. However, this rule also has exceptions. For example, consider the following statement:

If (c = 1 & e ++ <100) d = 100;

Here, the standard AND operator (single &) is used to ensure that e is incremental regardless of whether c is equal to 1.

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.