This note comes from b station crazy god said: chat compilation pilot class, interested partners point here: crazy god chat compilation pilot class

The nature of base

What is the nature of base? Base is the concept of a set of symbols and a number of times.

What does that mean? Let’s take a look at some data

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 # As you can see,0~10 is the symbol we define, and the next digit of 9 is 10, which means 1 for every 10-10. So NOW I can also define my own base, Lao Wang's base symbol is A, B, C, D, e stands for 1, 2, 3, 4, 5, And then you go 1 for every 5 and you get 0, 1, 2, 3, 4, 5, A, B, C, D, E, BA so the essence of the base system is a set of symbols plus the concept of what you go toCopy the code

This idea applies to the category of base encryption, after all, you define the base symbol and base algorithm, others do not know, decryption is difficult after all, but if the amount of data is large, any number can find the rule.

The difference between signed and unsigned numbers

Both signed and unsigned numbers are represented by 8-bit binary numbers

I.e., 0000, 0000

But how do you represent a negative number? From this we have signed and unsigned numbers

In unsigned numbers, the 8-bit value is its own value, for example: 0000, 1001 represents 9

In signed numbers, the first digit of the highest digit represents a positive (0) and a negative (1).

For example,

0000 1001-9

1001-1000-9

Troublesome source code, inverse code, complement code

These coding rules are all for signed numbers

The original code:

  1. Positive: Takes the absolute value of all bits except the first sign bit
  2. Negative numbers: Same as positive numbers

Radix-minus-one complement:

  1. Positive: same as the original code
  2. Negative number: the first bit is the sign bit, and the remaining bits are the negation of the source code

Complement:

  1. Positive: same as the original code
  2. Negative: the first bit is the sign bit, and the remaining bits are negative +1

Example:

1 Original code 0000 0001 Inverse code 0000 0001 Complement code 0000 0001-1 Original code 1000 0001 Inverse code 1111 1110 Divide the sign bit to obtain the inverse complement code 1111 1111 Original code to obtain the inverse + 1-7 original code 1000 0111 Inverse code 1000 1000 Divide the sign bit by the inverse complement code 1111 1001 The original code by the inverse +1Copy the code

An operation

All computer operations are carried out by bit operations

And (&)

In series, they’re all one

Only when both switches are off (1) does the bulb light up (1)

So it’s only going to be 1 if they’re both 1

# and (&) 1001 0011 1110 1010 ----------- 1000 0010Copy the code

Or (|)

In parallel, if you have one, light up

When either switch (1) is turned off, the bulb lights up

That is, if any one has a 1, the result bit is 1

# or (|) arithmetic, 1001, 0011, 1110, 1010 -- -- -- -- -- -- -- -- -- -- - 1111-1011Copy the code

Xor (^)

Double knife switch, need to be different to light

The bulb lights only when the switch status is different

It’s only one if it’s different

# xor (^) 1001 0011 1110 1010 ----------- 0111 1001Copy the code

Not (~)

According to the not

# Non operation (~) 1110 1010 ----------- 0001 0101Copy the code

Shift operation

Shift left (<<)

# move left, lower fill 0 1001 0111 --------- 0010 1110Copy the code

To move to the left is equivalent to a number *2, such as 101 (5) to move to the left – 1010 (10)

Moves to the right (> >)

# right shift, high complement symbol bit 1001 0111 --------- 1100 1011Copy the code

The right shift is the same thing as PI over 2

Add, subtract, multiply and divide with bit operations

The first thing to understand is that all addition, subtraction, multiplication and division are addition

Such as:

6-4 = 6+ (-4) 6*3 = 6+6+6 18/3 = how many times does 3+ =18Copy the code

Computer low-level addition:

4 + 5 =?

0000 0100 0000 0101 --------- # Add has only two steps, xor, and, and these two steps continue indefinitely until and results in 0 # first step: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0100 0000 0101 --------- 0000 0100 # And operation result left 0000, 0100 -- -- -- -- -- -- -- -- -- 0000, 1000 (left) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = to do cycle, 0000 0001 0000 1000 --------- # step 1, 0000 0001 0000 1000 --------- 0000 1001 # Step 2, 0000 0001 0000 1000 --------- 0000 0000 # if the result of the and operation is 0, then the last xOR result is 0000 1001 = "9"Copy the code

Example: 8-2 = “8+ (-2) =?

- 2: 1111 1110 0000 1000 1111 1110 --------- # xor 0000 1000 1111 1110 --------- 1111 0110 # and operation, Check the carry of 0000 1000 1111 1110 --------- 0000 1000 # need to carry, move left 0000 1000 --------- 0001 0000 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = circulation, 1111, 0110, 0001, 0000 -- -- -- -- -- -- -- -- -- # exclusive or 1111, 0110, 0001, 0000 -- -- -- -- -- -- -- -- -- 1110, 0110 1111 0110 0001 0000 --------- 0001 0000 # left 0001 0000 --------- 0010 0000 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = circulation, 1110, 0110, 0010, 0000 -- -- -- -- -- -- -- -- -- # exclusive or 1110, 0110, 0010, 0000 -- -- -- -- -- -- -- -- -- 1100, 0110 # and computation, 1110, 0110, 0010, 0000-0010 -- -- -- -- -- -- -- -- 0000 # left carry 0010 0000 -- -- -- -- -- -- -- -- -- 0100, 0000 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Loop 1100 0110 0100 0000 --------- # xor 1100 0110 0100 0000 --------- 1000 0110 # with operation 1100 0110 0100 0000 --------- 0100 # 0000 left carry 0000-0100 -- -- -- -- -- -- -- -- 1000, 0000 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = circulation, 1000, 0110, 1000, 0000 -- -- -- -- -- -- -- -- -- # exclusive or 1000 0110 1000 0000 --------- 0000 0110 # and operation judgment carry 1000 0110 1000 0000 --------- 1000 0000 # left 1000 0000 --------- 0000 0000 = = = = = = = = = = = = = = = = = = = = = = = = = = = circulation, 0000, 0110, 0000, 0000 -- -- -- -- -- -- -- -- - # 0000, 0110, 0000, 0000 of an exclusive or operation -- -- -- -- -- -- -- -- -- 0000, 0110 # and 0000 operations 0110 0000 0000 --------- 0000 0000 and if the result is 0, the result is 0000 0110:6Copy the code