Insist. Share. Grow. Altruism!

A preface.

Tai Chi gives birth to two instruments, two instruments give birth to four phases, four phases give birth to eight diagrams, eight diagrams determine good or bad luck

In fact, there is a very similar principle between The eight diagrams in China and the binary system in the West. But when it comes to evolution and calculation, gossip wins.

Yin: 0, Yang: 1;

Lao Yin: 00, Shaoyang: 10, Shaoyin: 01, Lao Yang: 11;

Kun: 000, Gen: 100, KAN: 010, Xun :110, Zhen: 001, Li :101, Dui: 011, Dry: 111;

2. Text.

A computer is a binary logical machine

Computer three big pieces: motherboard,CPU, memory; And the most important is the CPU, the main composition of the CPU can be understood as the transistor, the transistor is equivalent to a switch, that is, in the telegraph. And _. This binary simplicity also makes computers fast.

** Computers only know binary, so how do you make them useful? **

That isConvention greater than Configuration

First character set

  • ASCIICharacter set
  • ISO 8859-1Character set
  • GB2312Character set
  • GBKCharacter set
  • utf8Character set

A number, int as an example:

The decimal system binary
201314 0000 0000 0000 0011 0001 0010 0110 0010
– 201314. 1000 0000 0000 0011 0001 0010 0110 0010

Clever use of binary in source code (Java example)

Many sources use binary to distinguish between states, and states can be combined. Mainly using the & and | calculation.

0, 0 = 0; 0 | 0 = 0; 1 = 0 0 &; 0 | 1 = 1; 1 & 0 = 0; 1 | 0 = 1; 1 &1 = 1; 1 | 1 = 1;Copy the code

Such as:

Streams
public int characteristics(a) {
    // all features are included
    return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.IMMUTABLE | Spliterator.NONNULL |
           Spliterator.DISTINCT | Spliterator.SORTED;
}

public int characteristics(a) {
    if (beforeSplit) {
        / / some features: both common aSpliterator. Characteristics () & bSpliterator. Characteristics ()
        return aSpliterator.characteristics() & bSpliterator.characteristics()
               & ~(Spliterator.DISTINCT | Spliterator.SORTED
                   | (unsized ? Spliterator.SIZED | Spliterator.SUBSIZED : 0));
    }
    else {
        returnbSpliterator.characteristics(); }}Copy the code
public interface Spliterator<T> {
    public static final int ORDERED    = 0x00000010;

    public static final int DISTINCT   = 0x00000001;

    public static final int SORTED     = 0x00000004;

    public static final int SIZED      = 0x00000040;

    public static final int NONNULL    = 0x00000100;

    public static final int IMMUTABLE  = 0x00000400;

    public static final int CONCURRENT = 0x00001000;

    public static final int SUBSIZED = 0x00004000;
}
Copy the code

Binary header information is cleverly used in some records

In the mysqlCOMPACT row formatA record.

Clever use of the binary of an object in the JVM

Binary solves some interesting problems

There are 1000 barrels of wine, one of which is toxic. Once eaten, the toxicity takes about a week to set in. Now we use mice to do the experiment, to find the barrel of poisoned wine after a week, and ask how many mice are the minimum.

In fact, everyone has seen this problem. The idea of solving the problem is also clever use of binary to complete.

  • Ten mice were placed in a cage in order: 0-9

  • The 1000 bottles of medicine were numbered in turn and converted into binary, such as 8 = 1000, and the mice at the corresponding position drank the medicine according to the number of 1 appearing in binary. Because we chose 10

    Mice, 2^10 = 1024 > 1000, can ensure that all numbered wine can be fed to mice without missing bits of 1 converted to binary.

  • The long wait, the death of the mouse

  • The number of dead mice was recorded successively, and then in the binary number of 10 digits, the number of dead mice corresponding to the binary number of 10 digits was 1, and the number of undead mice corresponding to the binary number of 10 digits was 0, that is, the number of poison was known.