The underlying implementation of commonly used logical operators &&, || in Java

        In work, & and | are often used to perform logical operations. In fact, it is to get a Boolean result that meets the business needs after giving several Boolean results.

        For example:

boolean codeResult = checkCode("0717");  //返回true或者false
boolean passwordResult = checkCode("jkwang147.."); //返回true或者false

//业务要求需要两者都正确才允许登录
if(codeResult && passwordResult ){
    //两者都为true
    loginSuccess();
}


//此时产品经理说需求有改动,只要验证码(codeResult)和密码任意一个校验成功即可登录
if(codeResult || passwordResult ){
    //一者为true即可进入
    loginSuccess();
}

        Yes, this kind of judgment can be seen everywhere in the business. But how is it achieved?

        This has to mention the binary operators at the bottom of the computer.

binary operation

        First, let's learn about the second mechanism, and its operators. The bottom layer of the computer is actually 0 and 1. Our binary is data stored with 0 or 1.

        For example, we now have a value type 17 that we want to store in the computer. 

17:

0001 0001

1:

0000 0001

5:

0000 0101

        It will be stored in memory in this form. Each number is 1 bit.

        1bit = 1 bit 1 byte = 8bit So one byte can only store -128 ~ 127

  

 This is why we will report an error when the data of the int type is stored to a certain size. Because the int type has only 4 bytes.

OK, that's getting a bit far. Back to our usual bitwise operators.

Common operators are ! , &, |      these three kinds of not, and, or

Very common are ^ , >> , >>> etc.

bitwise operation

What are bitwise operations? In fact, it is very easy to understand. In fact, it is to take out the binary data of two values, and operate each bit on each bit.

1. ^ XOR operation

        1 if the bits are different, and 0 if they are the same. It will be more difficult to remember this way. We think of ^ as the eyes of a smiley face. 1 is male, 0 is female. When will you laugh? It means that boys will laugh when they see girls. So if the pairing is a man and a woman, he is happy and it is 1. If the match is the same sex, it is 0; let's do the following calculations.

Calculation: 2^5

2 in binary: 0010

Binary of 5: 0101

Calculation result: 0111 Restored binary result is: 7

2. | OR operation

        | 1 if both are 0 and 0 if both are 0. You can also use | as looking for boys. If there is a boy in the bit, it is 1

Calculations: 2 | 5 

2 in binary: 0010

Binary of 5: 0101

Calculation result: 0111 Restored binary result is: 7   

        Same result as XOR, but purely by coincidence, the two operations are not related.

3. & and operation 

        & Only when both bits are 1 is 1, otherwise it is all 0. &You can see it is very distorted, you can see it as deformed love. Only Nannan is 1.

Calculations: 2 & 5 

2 in binary: 0010

Binary of 5: 0101

Calculation result: 0000 Restore binary result to: 0

4.! not operation

       This operator is not the operator of two binary. It usually negates a binary. For example: 0000 becomes 1111.

1 is 0, 0 is 1 

Java Logical Operators & and | 

        Before that, let's talk about the boolean type. Because our logical operators and or are usually used with boolean value types. And the bottom layer of our boolean value is actually two values. 0 and 1 

        0 is false

        1 is true

So the common if(boolean a & boolean b) if(boolean a | boolean b) in our code is actually the bitwise operation of 0 and 1 at the bottom 

boolean t = true;
boolean f = false;

if(t & f){
    实则是:0001
            &
           0000
---------------------
       结果:0000
    返回的是0 所以是flase
}



if(t | f){
    实则是:0001
            |
           0000
---------------------
       结果:0001
    返回的是1 所以是true
}

Then why it becomes && and || in Java. A single one is a bitwise judgment. The double is a logical judgment.

Java for efficiency. Do not have to operate some unnecessary operations. So it encapsulates the logical AND &&, or ||.

For example, && logical AND here. We know that it is true to judge that both are true. But my boolean value is flase so there is no need. Go and calculate with the second boolean value.

|| : logical or also. When our first boolean value is true. It doesn't matter whether the second boolean value is true or not. Anyway, it returns true.

 

Guess you like

Origin blog.csdn.net/m0_58907154/article/details/130559256