On the popular understanding of logical operators

Source: Internet
Author: User

Now basically do not communicate with the hardware, software development very few people to understand these logical operators, often use some logical operators, today to tidy up, a common understanding of the various operators how the situation.

First put the test code, the code itself is meaningless, mainly on the results of the description

Class Program {static void Main (string[] args) {int a = 123, B = 0;            int result;            #region 1. Logical non (~) operation A = 123;            result = (byte) ~a;            Console.WriteLine ("{0} non (~) operation", a);                        Printresult (result, a, b);            #endregion #region 2. Logic and (&) operation B = 12;            result = A & B;            Console.WriteLine ("{0} and (&) {1} operations", A, b);            Printresult (result, a, b);            #endregion #region 3. Logical OR (|) operation result = A & B; Console.WriteLine ("{0} or (|)            Operation of {1} ", A, b);            Printresult (result, a, b);            #endregion #region 3. Logical XOR (^) operation result = a ^ b;            Console.WriteLine ("{0} xor (^) {1} operations", A, b);            Printresult (result, a, b);            #endregion #region 4. Left displacement (<<) operation A = 5;            b = 4; result = (byte) (a << b);            Console.WriteLine ("{0} Left shift (<<) {1}", A, b);            Console.WriteLine ("Result of operation: \t{0}", result);            Console.WriteLine (2 binary value for "{0}: \t{1}", A, FormatBase2 (a));            Console.WriteLine ("{0} of 2 binary values: \t{1}", result, FormatBase2 (result));            #endregion #region 5. Right displacement (>>) operation A = 5;            b = 1;            result = (byte) (a >> B);            Console.WriteLine ("{0} Right Shift (>>) {1}", A, b);            Console.WriteLine ("Result of operation: \t{0}", result);            Console.WriteLine (2 binary value for "{0}: \t{1}", A, FormatBase2 (a));                        Console.WriteLine ("{0} of 2 binary values: \t{1}", result, FormatBase2 (result));        #endregion Console.readkey ();            } static void Printresult (int result, int a, int b) {Console.WriteLine ("Result of operation: \t{0}", result);            Console.WriteLine (2 binary value for "{0}: \t{1}", A, FormatBase2 (a)); if (b > 0) {Console.WriteLine ("{0}" 2-inThe system value is: \t{1} ", B, FormatBase2 (b));            } Console.WriteLine ("{0} of 2 binary values: \t{1}", result, FormatBase2 (result));        Console.WriteLine ();            } static string FormatBase2 (int num) {string r = convert.tostring (num, 2);            while (R.length < 8) {r = "0" + R;        } return R; }    }

Explain that the logical operation of the bit, such as often said: a 8-bit byte, can be understood as 8 bits is a byte, usually used for some of the hardware permissions settings, such as: 11111111, a byte can control 8 permissions, it is really very provincial ah.

The following examples are related to this 2 binary, see 2 binary results will understand that the original name of these symbols is so intuitive.

First to popularize the basic knowledge of computers, computers are only known 0 and 1; That we call, true and false, or professional dot called true and False

1. Non-arithmetic C # operation symbol: ~, Windows Calculator operation symbol: not

123 Non-(~) operations
Operation Result: 132
123 of the 2 binary values are: 01111011
132 of the 2 binary values are: 10000100

According to the above results, clearly see that the original "non" operation, is so simple,

The popular saying is: If it is true (1), then it is False (0), if it is False (0) then it is true (1)

2. With the arithmetic C # symbol: &, calculator symbol: and

123 and (&) 12 operations
Operation Result: 8
123 of the 2 binary values are: 01111011
12 of the 2 binary values are: 00001100
8 of the 2 binary values are: 00001000

Based on the 2 binary results,

Popular Will: If All is "really (1)" Then is "true" or "false (0)".

3. Logic or (|) operation, calculator symbol: OR
Operation of 123 or (|) 12
Operation Result: 127
123 of the 2 binary values are: 01111011
12 of the 2 binary values are: 00001100
127 of the 2 binary values are: 01111111

Popular explanation: If there is a true, then it is true (1), otherwise it is false (0);

4. Logic xor (^) operation, calculator symbol: Xor
123 XOR (^) 12 operation
Operation Result: 119
123 of the 2 binary values are: 01111011
12 of the 2 binary values are: 00001100
119 of the 2 binary values are: 01110111

With respect to the description of the XOR, the test parameters have been changed, in order to present the 3 different possibilities.

The concept of XOR is: the same is False (0), the difference is true (1); This seems to be the way to explain it.

5. Left displacement (<<) arithmetic calculator symbol: LSH

5 left Shift (<<) 4
Operation Result: 80
5 of the 2 binary values are: 00000101
80 of the 2 binary values are: 01010000

One sentence: 4 zeros to the right

6. Right Shift (>>) arithmetic calculator symbol: RSH

5 Right Shift (>>) 1

Operation Result: 2
5 of the 2 binary values are: 00000101
2 of the 2 binary values are: 00000010

Explanation: Fill 4 zeros to the left

Actually see these results, almost no explanation, he is the "bit" to calculate, converted to 2 binary is very intuitive.

On the popular understanding of logical operators

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.