binary

0000 0000 0000 0000In order to show` is `withNegative ` `, our ancestors invented the original code of the **, the **'First on the left'Set it aside, store the symbols,We use 0 for positive numbersTo say,` ` negative with 1To represent theCopy the code

Integer to binary

About decimal integer to binary, actually very simple, remember a secret, can be

In addition to2Mod, reverse orderCopy the code

That is, divide the decimal number exactly by 2 to get the quotient and remainder, and then divide the quotient exactly by 2 to get the new quotient and remainder, and repeat until the quotient is equal to 0. The remainder obtained first as the high order of the binary number, and the remainder obtained later as the low order of the binary number, in order

For example, we convert decimal 55 to base 2

55 % 2 // 27 has a remainder of 1
27 % 2 // 13 leaves 1
13 % 2 // 6 leaves 1
6 % 2 // The remainder is 0
3 % 2 // 1 is left over 1
1 % 2 // The remainder of 0 is 1
Copy the code

Mod inverse, the result of 55 to 2 decimal is 110111

For example, if the 8-bit binary of decimal 55 is 00110111, then some people may also wonder what if it is 4 bits, 4 bits can not store such a large value of 55, overflow

Decimal to binary

For those of you who don’t know how to convert decimal to binary, there’s a formula, right

Round by two, order copy codeCopy the code

In 2 decimal fraction, can get the product, the product of the integer part, and in 2 by the rest of the decimal part, and get a product, and then remove the integer part of the product, and so on, until all the integer part is zero, the product or the integer part is 1, 0 or 1 at this time is the last of the binary or to achieve the required accuracy, The extracted integer parts are then arranged in order, with the first integer as the highest significant bit of the binary decimal and the second integer as the lowest significant bit

For example, convert the decimal number 0.625 to binary

0.625 * 2 = 1.250 // Take the integer 1. 0.25 * 2 = 0.50 // Take the integer 0. 0.5 * 2 = 1 // Take the integer 1 and end copying the codeCopy the code

Rounded order, then the binary of the decimal 0.625 is 0.101

If the decimal value is a decimal greater than 1, then the integer part and the decimal part can be concatenated by binary

For example, convert the decimal number 5.125 to binary

Let’s start with the binary of the integer 5

5% 2 // shang 2 + 1 2% 2 // Shang 1 + 0 1% 2 // Shang 0 + 1 copy codeCopy the code

So the binary of 5 is 101, and then the decimal part

0.125 * 2 = 0.250 // Take the integer 0. 0.25 * 2 = 0.50 // Take the integer 0. 0.5 * 2 = 1 // Take the integer 1 and end copying the codeCopy the code

The binary of the decimal part 0.125 is 001, and the concatenation of the decimal number 5.125 is 101.001

There is also a case where, for example, the decimal number 0.1 is binary

0.1 * 2 = 0.2 // The integer 0 0.2 * 2 = 0.4 // The integer 0 0.4 * 2 = 0.8 // The integer 0 0.8 * 2 = 1.6 // The integer 1 0.6 * 2 = 1.2 // The integer 1 -> 0.2 * 2 = 0.4 // integer 0 0.4 * 2 = 0.8 // integer 0... Copy the codeCopy the code

So its binary is 0.0001100…… This repeated cycle, which also leads to our problems in the language level, such as JS is criticized 0.1 + 0.2! = 0.3, we’ll talk about that later

Please refer to the above sources for detailed explanation – link address: juejin.cn/post/689794…

Result of bit logic operation

Formula:

Bit operation: &

12 divided by 8 is 8

        0000 0000 0000 1100         12Binary &0000 0000 0000 1000          8Binary -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --0000 0000 0000 1000Results:8Binary reference formula graph, from the first digit one by one comparison results, the result is8The binary, decimal and8

Copy the code

Or operation: |

4 and 8 is 12

        0000 0000 0000 0100          4The binary |0000 0000 0000 1000          8Binary -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --0000 0000 0000 1100Results:12Binary reference formula graph, from the first digit one by one comparison results, the result is12The binary, decimal and12

Copy the code

An exclusive or operation: |

31 and 22 is 9

        0000 0000 0001 1111          31Binary ^0000 0000 0001 0110          22Binary -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --0000 0000 0000 1001Results:9Binary reference formula graph, from the first digit one by one comparison results, the result is9The binary, decimal and9

Copy the code

To take an operation: |

Minus 123, minus 124

~       0000 0000 0111 1011          123Binary -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --1111 1111 1000 0100Results:9Binary reference formula graph, from the first digit one by one comparison results, the result is9The binary, decimal and9

Copy the code

Binary to decimal

    0000 0000 0000 1101Corresponding by digit:1*2DHS +0*2Creates a +1*2Squared plus1*2After =13
Copy the code

2 to the zero power is 1 (any number raised to the zero power is 1) \

Reference formula: