Difference between bit operations (&, |, ^) and logical operations (&, |)

Source: Internet
Author: User

I just saw the introduction of bitwise operations (&, |) and logical operations (&, |) in an article, and remembered my weak basic knowledge, so Baidu summarized several points.

First, conceptually, bitwise operations are performed by converting the numbers on both sides of the operators into binary values (for example, 0000010001) and comparing the values 0 and 1 at the same position. Logical operations are the logical values (true or false) on both sides of the comparison operator ). The concept is abstract, and the following is a comparison with the actual example.

Bitwise operation

First, convert each number to a binary value. Then, bitwise OR (|) indicates that at least one of the corresponding numbers is 1, and the result is 1. Only two numbers are 0, the result is 0. bitwise AND (&) indicate that the two corresponding values must be 1 and the result is 1. Otherwise, the result is 0. Bitwise OR (^) is special. If two differences exist, the value is 1 (for example, 1, 0) (0, 1 )), if the values are the same (for example, 1, 1) (0, 0), the values are 0.
Example: 3 | 2
11 // binary 3
10 // binary 2
--------------------
11 // binary 3

Example: 3 & 2
11
10
---------------------
10 // binary 2


Example: 3 ^ 2

11

10

---------------------

01 // binary 1

This example describes the principle of bitwise operations. Note that bitwise operations here | result 3, and result 2 are just a coincidence that the number 2 and 3 are repeated, do not assume that the bitwise operation result is one of two numbers.

Logical operation

Convert the expressions on both sides of the logical operator (& |) to logical values (true and false). The logical value or (|) indicates that if either of the two values is true, the result is true. If both are false, the result is false. Both logic and (&) indicate true, and true is returned. Otherwise, false is returned. This is simple, so we will not introduce it as an example.



Function

I believe everyone knows the role of logical operations. The following describes the role of the next bit operation:

I. bitwise AND &
1. Reset the special location (0 for the specific position in the mask, 1 for the other bits, S = S & Mask) S = S & Mask
2. Locate a specified position in a specified number (mask: 1, other bits are 0, S = S & Mask) S = S & Mask
2. bitwise OR |
It is often used to change the source operand to some positions of 1, and the other BITs remain unchanged.
3. bitwise OR
1. Reverse the value of the specific location (1 in the specific position of mask and 0 s = s ^ mask in the other bit)

2. Auto-Zeroes a number. For example, a = a xor a, No matter what a is, A is equal to zero.



To make the role of bitwise operations clearer, let's take another example:

Bitwise AND operations are usually used to clear some bits or retain some bits.

For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).

High eight bits clear 0, keep the low eight bits, as long as the first eight bits of a number are 0 (& when one is 0, it means that the value is cleared) the last eight digits are 1 (the value after 1 is compared with the original number (0, 1) is also the value of the original number, meaning that the original value is retained), then the number is 0000000011111111, that is, 255.


Difference between bit operations (&, |, ^) and logical operations (&, |)

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.