Talk about Java-bit computing

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u010695794/article/details/88262159

This article first appeared in the personal micro-channel public number "andyqian", look forward to your attention ~

Foreword

   We all know that in the computer world, complex, and then the United States program, in the end will become 0 and 1. That is, we often say: binary . Binary I believe we are familiar with. The difference is that with the real world, in the real world, we are usually expressed as a decimal, that is, in case of a decimal, these are familiar to us. Here, we will find clues to the real world with computer binary decimal metering unit which is not the same. Then how to convert between them? This involves some of the more basic computer knowledge. We are not discussed in this article (if interested, you can talk about the next time). Ah, back to today's topic, it said bit computing, this is a kind of concept? We were brought came into contact with the real world of these arithmetic operations, that is, decimal arithmetic. Today we are saying is: bits of some common operations. For example: & (and bit) , | (or bits)  , ^ (XOR) , << (left shift) , >> (right)  and so on.

True and false

  Before making operator use, we need to say that the next true and false. In Java, we all know, the value indicated by true true, false value represents false. In fact, in the computer, usually 1 for true, 0 for false. Used Json students should know that the Java boolean type, use 1 can also deserialized into true, 0 deserialized to false.

& (Bits of)

   Bit before saying, Let's say we are familiar  &&  logical AND operation. In short: A && B is: When A and B while the establishment of true and false otherwise. It was called: "a false must be false."

Now we look at the position. First, let's look at a program:

 @Test
    public void testBit(){
        int a = 8;
        int b = 9;
        System.out.println("a binary: "+Integer.toBinaryString(a));
        System.out.println("b binary: "+Integer.toBinaryString(b));
        System.out.println("a & b binary: "+Integer.toBinaryString(a&b));
        System.out.println("a & b result: "+(a&b));
    }

Look explained before, let's guess how much is the result?

Code explanation :

Bit with: us up from the literal meaning of understanding, but also bits and operation.

  1. 8 decimal numbers are: 1000.

  2. Decimal digit is 9: 1001.

 

We come to be located in operation:

As follows:

8:1000
9:1001
&
8  1000

& Leftmost 1 1 = 1, 0 & middle 0 = 0, 0 & rightmost 1 = 0.

Binary result: 1000, converted to decimal 8.

Program results are as follows:

a binary: 1000
b binary: 1001
a & b binary: 1000
a & b result: 8

The result is in line with expectations.

| (Or bits)

  It says  & (bits of)  operation, and now we take a look at the operating position or continue to use the example above: as follows:

 @Test
    public void testBit(){
        int a = 8;
        int b = 9;
        System.out.println("a binary: "+Integer.toBinaryString(a));
        System.out.println("b binary: "+Integer.toBinaryString(b));
        System.out.println("a & b binary: "+Integer.toBinaryString(a|b));
        System.out.println("a & b result: "+(a|b));
    }

Look at the binary:

8:1000
9:1001
|
9  1001

The leftmost 1 | 1 = 1, the middle of the 0 | 0 = 0, the rightmost 0 | 1 = 1.

Results for the binary: 1001 corresponding to 9 decimal.

Calculation results are as follows:

a binary: 1000
b binary: 1001
a & b binary: 1001
a & b result: 9

^ (Exclusive OR)

  This operator is more interesting, isobutyl understood literally: Different. Bit operating on the inside is the same. Continue Using the above example:

@Test
    public void testBit(){
        int a = 8;
        int b = 9;
        System.out.println("a binary: "+Integer.toBinaryString(a));
        System.out.println("b binary: "+Integer.toBinaryString(b));
        System.out.println("a & b binary: "+Integer.toBinaryString(a^b));
        System.out.println("a & b result: "+(a^b));
    }

Continue to look at binary:

8:1000
9:1001
^
1  0001

Phase while taking false, not true at the same time take. 1 = 1 is the same on the left to take leave, which is 0. The intermediate is also false 0 0 = 0. The rightmost 0 does not equal 1, is true. The results also 1.

<< Left

  In the real world, we often use multiplication. << indicates a binary shift operation, low 0s. For example: 8 << 1.

@Test
    public void testCode(){
        int a =8;
        System.out.println("a toBinaryString: "+Integer.toBinaryString(a));
        System.out.println("a<<1 toBinaryString: "+Integer.toBinaryString(a<<1));
        System.out.println("result: "+(a<<1));

Binary as follows:

8  1000
8<<1
16 10000

Results: 2 ^ 4 = 16. << represents the left side of a base, to the right the number of bits needed a place to move. Arrows point to which side, which side is displaced. Result of the program:

a toBiryString: 1000
a<<1 toBinaryString: 10000
result: 16

>> Right

  >> (right) and left << is the opposite, high 0s. Continue the example above:

@Test
    public void testCode(){
        int a =8;
        System.out.println("a toBinaryString: "+Integer.toBinaryString(a));
        System.out.println("1>>a toBinaryString: "+Integer.toBinaryString(a>>1));
        System.out.println("result: "+(a>>1)
    }

Binary:

8 : 1000
8>>1
4 : 0100

operation result:

a toBinaryString: 1000
a>>1 toBinaryString: 100
result: 4

In fact, there's a relatively easy to remember formulas:

a >> n indicates: a / (2 ^ n) th power. (Rounding)

a << n the result is: a * (2 ^ n) th power.

Now let's fast count:

When a = 13, n = 2. << 2 13 13 * 4 = 52 equal. 13/4 = 3.

(Above speed algorithm, if wrong, to welcome her face !!!)


 

   We source and displacement operations common algorithm is very common, a Java programmer to master bit computing is necessary. It is very helpful to our algorithm, source code to understand!


 

Related Reading:

" Thousands of lines of stored procedure felt! "

" Software of the road ."

" On the JPDA the Java "

" Talk about MySQL privilege "

                                                                                     Scan code concerned, together with progress

                                                                        Personal blog: http://www.andyqian.com
 

Guess you like

Origin blog.csdn.net/u010695794/article/details/88262159