& Amp; and & amp,

Source: Internet
Author: User

& Differences,

In programming, & is interpreted as: & is a logical operator, and & is a bitwise operator. But what are the differences in their usage?

1. When & and & are used as logical operators, let's see what they all do. See the following example:

1  int i = 8;2  int j = 8;3  int k = 8;4  int h = 8;5  bool a = (i > 5) && ((i = i + 5) > 10); //................(1)6  bool b = (j > 5) & ((j = j + 5) > 10);  //................(2)7  bool c = (k > 15) && ((k = k + 5) > 10);//................(3)8  bool d = (h > 15) & ((h = h + 5) > 10); //................(4)

See the output result:

For (1), if I> 5 is True and (I = I + 5)> 10 is True, a is True, that is, if both are True, a is True. At this time, I = 13.

For (2), if j> 5 is True and (j = j + 5)> 10 is True, B is True, that is, if both are True, a is True. In this case, j = 13.

For (3), if k> 15 is False and (k = k + 5)> 10 is True, c is False, that is, if it is set to True or False, c is set to False. K = 8.

For (4), if h> 15 is False and (h = h + 5)> 10 is True, d is False, that is, if the value is True or False, the value d is False. H = 13.

A sentence can be summarized as follows:

&: Logical and. If two values are True, they are True. If one value is False, they are False. That is, two are true, and one is false.

&: Logical and. If two values are True, True is used. If one value is False, False is used. That is, two are true, and one is false.

Differences:

Expression (1) & Expression (2). Expression (2) is executed only when expression (1) is True.

Expression (1) & Expression (2), whether expression (1) is True or False, expression (2) is executed.

2. When & acts as a bitwise operator, let's see what it does. See the following example:

1 int I = 6; // The binary value of 6 is 01102 3 I = I & 5; // The binary value of 5 is 0101.

See the output result:

When the & operator is a bit, bitwise operations are performed on 6 and 5, while the binary value of 6 is 0110, and the binary value of 5 is 0101. After logic &, the binary value of I is 0100, the corresponding decimal value is 4.

Related Article

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.