Home > Backend Development > PHP Tutorial > PHP basic tutorial

PHP basic tutorial

巴扎黑
Release: 2016-11-21 10:47:16
Original
1298 people have browsed it

Detailed explanation: Binary, octal, decimal and hexadecimal conversion 1. Conversion between decimal and binary
 (1) Decimal to binary conversion, divided into integer part and decimal part
 ① Integer part
 Method: divide by 2 Remainder, arranged in reverse order, that is, each time the integer part is divided by 2, the remainder is the number on the position weight, and the quotient continues to be divided by 2, and the remainder is the number on the previous position weight. This step continues until the quotient to 0. When reading the last number, read from the last remainder to the first remainder. Here is an example:
 Example: Convert decimal 168 to binary
 Get the result Convert decimal 168 to binary, (10101000)
 The first step is to divide 168 by 2, the quotient is 84, and the remainder is 0.
 The second step is to divide the quotient 84 by 2, and the remainder of the quotient 42 is 0.
 The third step is to divide the quotient 42 by 2, and the remainder of the quotient 21 is 0.
 The fourth step is to divide the quotient 21 by 2, and the remainder of the quotient 10 is 1.
 The fifth step is to divide the quotient 10 by 2, and the remainder of the quotient 5 is 0.
 The sixth step is to divide the quotient 5 by 2, and the remainder of the quotient 2 is 1.
 The seventh step is to divide the quotient 2 by 2, and the remainder of the quotient 1 is 0.
Step 8: Divide the quotient 1 by 2, and the remainder of the quotient 0 is 1.
 Step 9, read, because the last digit is obtained after dividing by 2 many times, so it is the highest digit. Read the number forward from the last remainder, that is, 10101000
 (2) Decimal part
 Method: Multiply 2 Round to integer, arrange in order, that is, multiply the decimal part by 2, then take the integer part, continue to multiply the remaining decimal part by 2, then take the integer part, multiply the remaining decimal part by 2, and take the decimal part
until it reaches zero. If it can never be zero, it is the same as the rounding of decimal numbers. When retaining as many decimal places as required, the number will be rounded based on whether the next digit is 0 or 1. If it is zero, round it off. If it is 1, add one digit. In other words, 0 is rounded to 1. The reading should be read from the previous integer to the following integer. Example 1: Convert 0.125 to binary , get 0.25, then the integer part is 0, and the decimal part is 0.25;
 The second step, multiply the decimal part 0.25 by 2, and get 0.5, then the integer part is 0, and the decimal part is 0.5;
 The third step, multiply the decimal part Multiply 0.5 by 2 to get 1.0, then the integer part is 1 and the decimal part is 0.0; The fourth step is to read from the first digit to the last digit, which is 0.001.
 Example 2, convert 0.45 to binary (retained to the fourth decimal place)
 As you can see from the above steps, when you do the multiplication for the fifth time, the result is 0.4, then the decimal part continues to be multiplied by 2 to get 0.8 , 0.8 multiplied by 2, keep multiplying until 1.6, and it is impossible to get the decimal part to be zero in the end. Therefore, at this time, we have to learn the decimal method to round, but binary only has 0 and 1, so 0 appears. Rounded up. This is also caused by computer errors during conversion, but since there are many reserved digits and the accuracy is very high, it can be ignored.
 Then, we can conclude that converting 0.45 to binary is approximately equal to 0.0111
 The method introduced above is a method of converting decimal to binary. What you need to pay attention to is:
 1) To convert decimal to binary, it needs to be divided into integers and decimals. The parts are converted separately
  2) When converting integers, the division by 2 method is used, and when converting decimals, the multiplication by 2 method is used
  3) Pay attention to their reading directions
Therefore, from the above method, we We can conclude that the decimal number 168.125 converted to binary is 10101000.001, or the decimal number converted to binary is approximately equal to 10101000.0111.
 (3) Convert binary to decimal without dividing the integer and decimal part
 Method: Add by weight, that is, multiply the number in each bit of the binary by the weight, and then add the sum to the decimal number. Example
 Convert the binary number 101.101 to a decimal number.
 The result is: (101.101)2=(5.625)10
 What everyone needs to pay attention to when converting binary to decimal is
 1) You must know the weight of each bit of binary
 2) You must be able to find the value of each bit
 2 , Conversion between binary and octal
First, we need to understand a mathematical relationship, that is, 23=8, 24=16, and octal and hexadecimal are derived from this relationship, that is, expressed in three-digit binary One octal digit represents a hexadecimal number using four binary digits.
Next, remember the four numbers 8, 4, 2, 1 (23=8, 22=4, 21=2, 20=1). Now let's practice converting between binary and octal.
 (1) Convert binary to octal
Method: Take the three-in-one method, that is, starting from the decimal point of the binary system as the dividing point, take every three digits to the left (right) into one digit, and then add these three binary digits according to weight, and the resulting number is a one-digit, eight-digit binary system. The numbers are then arranged in order, the position of the decimal point remains unchanged, and the number obtained is the octal number we are looking for. If you take three digits to the left (right) and get to the highest (lowest) digit, if you cannot make up the three digits, you can add 0 to the leftmost (rightmost) of the decimal point, that is, the highest (lowest) digit of the integer. Make up three people. Example
 ①Convert the binary number 101110.101 to octal
 Get the result: Convert 101110.101 to octal to 56.5
 ② Convert the binary number 1101.1 to octal
 Get the result: Convert 1101.1 to octal to 15.4
 ( 2) Convert octal to binary
Method: Take the one-by-three method, that is, decompose an octal number into three binary digits, and use the three-digit binary digits to add the octal number according to the weight. The decimal point position remains the same. Example:
 ① Convert the octal number 67.54 to binary
  Therefore, convert the octal number 67.54 into the binary number 110111.101100, that is, 110111.1011
  As you can see from the above question, calculate the conversion of octal to binary
  First, convert the octal number into binary according to the From left to right, each digit is expanded to three digits, and the decimal point position remains unchanged
Then, each digit is expanded to three digits 22, 21, and 20 (i.e. 4, 2, 1) to make up the number, that is, a×22+ b×21 +c×20=the number in the bit (a=1 or a=0, b=1 or b=0, c=1 or c=0), arrange abc to get the binary number of the bit
Then, put each Convert the bits into binary numbers and arrange them in order
Finally, you get the number converted from octal to binary.
 The above method is the exchange of binary and octal. What you need to pay attention to when doing the questions is
 1) The exchange between them is the conversion between one digit and three digits, which is different from the conversion between binary and decimal
 2 ) When adding 0 and removing 0, everyone should pay attention to adding 0 or removing 0 at the far left of the decimal point or the far right of the decimal point (that is, the highest digit of the integer and the lowest digit of the decimal), otherwise an error will occur
  3. Binary to hexadecimal conversion
 Method: Similar to binary to octal conversion, except that it is a conversion of one digit (hexadecimal) and four digits (binary). The following will explain in detail
 (1) Binary to hexadecimal conversion
Method: Take the four-in-one method, that is, starting from the binary decimal point as the dividing point, take every four digits to the left (right) into one digit, and then add these four binary digits according to weight, and the resulting number is a one-digit sixteen binary numbers, and then arrange them in order, keeping the position of the decimal point unchanged, and the resulting number is the hexadecimal number we are looking for. If you take four digits to the left (right) and get to the highest (lowest) digit, if you cannot make up the four digits, you can add 0 to the leftmost (rightmost) of the decimal point, that is, the highest (lowest) digit of the integer. Make up four people.
 ①Example: Convert binary 11101001.1011 to hexadecimal
 Get the result: Convert binary 11101001.1011 to hexadecimal to E9.B
 ②Example: Convert 101011.101 to hexadecimal
 So get the result: Convert binary 101011.10 1 Convert to hexadecimal to 2B.A
  (2) Convert hexadecimal to binary
 Method: Take the one-quarter method, that is, decompose a hexadecimal number into four binary digits, and use four binary digits The hexadecimal number is obtained by adding the weights, and the decimal point position remains the same.
 ①Convert hexadecimal 6E.2 to binary number
 So you get the result: Convert hexadecimal 6E.2 to binary to 01101110.0010, which is 110110.001
 IV. Conversion between octal and hexadecimal
 Method: Generally not possible Direct conversion between each other usually involves converting octal (or hexadecimal) to binary, and then converting binary to hexadecimal (or octal), with the decimal point position unchanged. Then for the corresponding conversion, please refer to the above conversion of binary to octal and conversion of binary to hexadecimal
5. Conversion of octal to decimal
The numbers above are multiplied by the bit weights and then added together to get the decimal number.
 Example: ①Convert octal number 67.35 to decimal
 (2) Convert decimal to octal
There are two methods to convert decimal to octal:
 1) Indirect method: first convert decimal to binary, and then convert binary to octal
2) Direct method: As we said before, octal is derived from binary, so we can use a method similar to converting decimal to binary, or the conversion of the integer part and the conversion of the decimal part. Let’s explain it in detail below:
 ①Integer part
Method: Divide by 8 and take the remainder method, that is, divide the integer part by 8 each time, the remainder is the number on the place weight, and the quotient continues to be divided by 8, the remainder is the number on the previous place weight, and this step continues , until the quotient is 0. When reading the last number, start from the last remainder to the first remainder.
  ② Decimal part
Method: Multiply by 8 to round up the whole method, that is, multiply the decimal part by 8, then take the integer part, continue to multiply the remaining decimal part by 8, then take the integer part, and multiply the remaining decimal part by 8. Keep taking until the decimal part is zero. If it can never be zero, it is the same as the rounding of decimal numbers, temporarily named 3 rounding.
 Example: Convert the decimal number 796.703125 to octal number
 Solution: First divide this number into the integer part 796 and the decimal part 0.703125
 The integer part
 The decimal part
Therefore, the result of converting the decimal number 796.703125 to the octal number is 1434.55
You can use the above method To verify, you can first convert decimal and then convert to octal to see if the results are the same
6. Conversion between hexadecimal and decimal
There are many similarities between hexadecimal and octal, you can refer to For the above conversion between octal and decimal, try the conversion between these two systems yourself.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template