Links and differences between && and | |,& in c,c#,c++

Source: Internet
Author: User
Tags bitwise bitwise operators

This article from: http://www.cnblogs.com/GT_Andy/archive/2010/03/30/1921805.html

The results are the same (for the respective operands), but there is a difference in performance.

&& | | : Logical operators

& and |: Bitwise operators

&& is meant to be, and a&&b both are true.
|| Yes or mean, a| | b There is one truth and that is true.

&,| is a bitwise operator. That is, the bitwise operation,
such as 00000011 & 00000001=00000001

for (&&,| |), the object of the operation is a logical value, that is, the True/false
operation results in only the following four cases.
true  && true  = True
true  && false = False
false && true  = false< Br>false && false = False

true  | | true  = True
true  | | False = True
False | | true  = True
False | | False = False

for (&,|), the object of the operation is a bit, that is, the result of the 1/0
operation is only the following four cases.
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

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

&& and & the result is the same for their respective operands.
Similarly, | | and | is the same.

For example: The result of the 5&&2 operation is treated like this.
First 5, nonzero, which is true
2, nonzero, true ,
True&&true = True
The result is true. The whole process of operation needs to be transformed. The binary direct comparison of operands is not directly used. So the results are different. At the time of comparison, true should be converted to a binary value like 11111111, false may turn into 00000000. The

is the same here, that is, for an operand.
&& 's operand is true/false    regardless of what type of expression you are using, he first converts to one of the true/false before participating in the operation.
& Operand is 0/1, do not need to convert, how many bits directly to calculate the number of bits on it.

Links and differences between && and | |,& and | In the c,c#,c++

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.