The conversion of C + + in the system

Source: Internet
Author: User
Tags decimal to binary

Binary, octal, decimal, hexadecimal conversion between decimal and binary conversions   (1) Decimal conversion to binary, divided into integral part and fractional part  ① Integer part   method: Except 2 take the remainder method, that is, each time dividing the integer part by 2, The remainder is the number of the right, and the quotient continues to divide by 2, and the remainder is the number of the last bit, this step continues until the quotient is 0, and the last reading, from the last remainder, is read to the first remainder. The following example: : Converting decimal 168 to binary    the result of converting decimal 168 to binary, (10101000) 2  analysis: The first step is to divide 168 by 2, quotient 84, and the remainder to 0.   The second step, divide the quotient 84 by 2, the quotient 42 is 0.   The third step, divide the quotient 42 by 2, the quotient 21 is 0.   Fourth, divide quotient 21 by 2 and quotient 10 to 1.   Fifth, divide quotient 10 by 2 and quotient 5 to 0.   Sixth, divide quotient 5 by 2 and quotient 2 to 1.   Seventh, divide quotient 2 by 2 and quotient 1 to 0.   Eighth, divide quotient 1 by 2 and quotient 0 to 1.   Nineth step, reading, because the last one is after multiple times divided by 2 to get, so it is the highest, read the number from the last remainder of the reading, that is, 10101000   (2) Decimal part   method: Multiply 2 Rounding method, will be the number of parts multiplied by 2, Then take the integer part, the remaining fractional part continues to multiply by 2, then take the integer part, the remaining fractional part is multiplied by 2, until the fractional part   is zero. If you can never be zero, just as the decimal number of rounding, as required to retain how many decimal places, according to the following one is 0 or 1, trade-offs, if it is zero, discard, if it is 1, into a bit. In other words, 0 shekels 1. Readings to be read from the previous integer to the following integer, example:  Example 1: Convert 0.125 to binary    results: Convert 0.125 to Binary (0.001) 2  analysis: The first step, 0.125 times 2, 0.25, Then the whole number of parts is divided into 0, the number of parts divided into 0.25;  the second step, the small part of 0.25 times 2, 0.5, the whole number of parts is divided into 0, the number of parts divided into 0.5;  the third step, the number of parts 0.5 times 2, 1.0, the whole number of 1, the number of parts divided into 0.0;   Fourth step, reading, reading from the first, reading to the last one, that is, 0.001.     Example 2, converting 0.45 to binary (reserved to decimalFour-bit)     we can see from the above steps, when the fifth time to do multiplication, The result is 0.4, then the fractional part continues to multiply by 2, 0.8, 0.8 and multiplied by 2, to 1.6 this has been riding down, the last possible to get a fractional part of zero, so this time had to learn the method of decimal rounding, but the binary system only 0 and 12, so it appears 0 1 into. This is also the computer in the conversion will produce errors, but because of the number of reserved bits, high precision, so it can be ignored.   So, we can conclude that converting 0.45 to binary restriction equals 0.0111 . The method described above is the decimal conversion method to binary, it is important to note that:  1) decimal conversion to binary, need to be divided into integers and two parts of the fractional conversion  2) When converting integers, use the addition of 2 to take the remainder method, and convert the decimal time, using the 2 rounding method  3) note their reading direction   Therefore, we from the above method, we can draw the decimal number 168.125 converted to binary 10101000.001 , or the decimal number is converted to a binary number that is approximately equal to 10101000.0111.    (3) binary conversion to decimal integer and Fractional part   method: By weighted addition method, the number of binary each bit is multiplied by the right, then the sum is the decimal number. Example   Converts a binary number 101.101 to a decimal number.    results: (101.101) 2= (5.625) 10  Everyone in doing binary conversion to decimal need to note is  1) to know the value of each bit of binary  2) to be able to find the values of each bit    Conversion between   two or two 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, a three-bit binary representation of an octal, with four-bit binary representation of a hexadecimal number.   Then, remember 4 digits 8, 4, 2, 1 (23=8, 22=4, 21=2, 20=1). Now let's practice the conversion between binary and octal.   (1) binary conversion to octal   method: Take the three-way method, that is, from the decimal point of the binary points, to the left (right) every three bits taken into one, and then the three-bit binary by the right to add, the number is a eight-bit binary number, and then, in order to arrange, the position of the decimal point unchanged, The number we get is the octal number we're asking for. If you take three bits to the left (right), the highest (lowest) bit, if you cannot fill up the three-bit, you can add 0 to the leftmost (rightmost) of the decimal point, which is the highest bit of an integer (the lowest bit), and make up three bits. Example  ① converting a binary number 101110.101 to octal    getting results: converting 101110.101 to octal to 56.5  ② converts binary number 1101.1 to octal    Get results: Convert 1101.1 to octal to 15.4   (2) Convert octal to binary   method: Take a three-way, will be a octal number decomposition into three-bit binary number, with three-bit binary right add to the octal number, the decimal place as usual. Example:  ① converting octal number 67.54 to binary    Therefore, the octal number 67.54 is converted to a binary number of 110111.101100, that is 110111.1011  everyone from the above problem can be seen, Calculate octal to binary   first, octet is set from left to right, each bit expands to three bits, the decimal point is not changed   then the 22,21,20 (i.e. 4, 2, 1) three bits per bit is expanded to do dine, that is, ax22+ bx21 +cx20= the number on the bit (a= 1 or a=0,b=1 or b=0,c=1 or c=0), the ABC arrangement is the binary number of the bit   next, convert each bit to binary number in order   Finally, the octal binary is converted to binary numbers.   above is the binary and octal interchange, you need to pay attention when doing the problem is  1) their interchange between a and three-bit conversion, this is different from the binary and decimal conversion  2) Everyone in the Tim 0 and go 0 should pay attention to, is at the far left of the decimal point or at the far right of the decimal point (that is, the highest bit of an integer and the lowest bit of a decimal) to add 0 or 0, otherwise error    three or two hexadecimal conversion   method: Similar to binary and octal conversions, is just a (16) and four-bit (binary) conversion, the following specific explanation   (1) binary conversion to hex   method: Take four-in-one method, that is, from the decimal point of the binary is the dividing point, left (right) every four bits, and then the four-bit binary right add, The resulting number is a 16-bit binary number, then, in order, the position of the decimal point is unchanged, the resulting number is the hexadecimal number we ask for. If you take four bits to the left (right) and take the highest (lowest) bit, if you cannot make up four bits, you can add 0 to the leftmost (rightmost) of the decimal point, the highest bit of the integer (the lowest bit), and fill four bits.  ① Example: theBinary 11101001.1011 to hex    get results: Convert binary 11101001.1011 to 16 E9.b   ② example: convert 101011.101 to hexadecimal    so get the result: Convert binary 101011.101 to 16 binary to 2b.a     (2) Convert 16 binary to binary   method: Take a four-way, A hexadecimal number is decomposed into a four-bit binary number, using four-bit binary to add the hexadecimal number, the decimal place as usual.  ① converts hexadecimal 6e.2 to binary    results: Converts hexadecimal 6e.2 to binary 01101110.0010 that is 110110.001   four or eight hexadecimal conversion.   methods: Generally do not convert directly to each other, generally, octal (or hexadecimal) is converted to binary, and then the binary is converted to 16 binary (or octal), the decimal place is unchanged. Then the corresponding conversion please refer to the above binary and octal conversion and binary with hex     five or eight binary with decimal conversion   (1) octal conversion to decimal   method: Weighted Add method, the number of octets per bit multiplied by the right , and then in addition to and that is the decimal number.   Example: ① convert octal number 67.35 to decimal    (2) decimal conversion to octal   decimal to octal there are two methods:  1) Indirect method: Convert decimal to binary first, then convert binary into octal  2) Direct method: As we have said before, octal is derived from binary, so we can take the decimal conversion to binary similar method, or integer part of the conversion and fractional part of the conversion, the following specific explanation:  ① integer part   Methods: In addition to the 8 method, that is, each time the integer part divided by 8, the remainder is the number of the right, and the quotient continues to divide by 8, the remainder is the number of the last bit, this step has been continued until the quotient is 0, the last reading, from the last remainder, up to the front of a remainder.  ② Fractional Part   method: Multiply 8 Rounding method, the fractional part is multiplied by 8, then take the integer part, the remaining fractional part continues to multiply by 8, then takes the integer part, the remaining fractional part is multiplied by 8, has taken to the fractional part is zero until then. If it can never be zero, just as the decimal number of rounding, the provisional name is called 3 4 in.   Example: the decimalNumber 796.703125 to octal number   solution: First divide this number into integer part 796 and fractional part 0.703125  integer portion    Fractional part    therefore, Get results decimal 796.703125 convert octal to 1434.55  above method you can verify that you can convert the decimal first, and then convert it to octal, so that you see whether the result is the same    66 or 16 decimal conversion   hexadecimal and octal have a lot of similarities, you can refer to the above octal and decimal conversion yourself to try the conversion between the two binary.   Through the above on the conversion between the various systems, we can change the previous conversion diagram:    This article introduces the binary, decimal, octal, hexadecimal four of the conversion between each other, we should pay attention to the conversion of the method, As well as the steps, especially the decimal conversion period between three kinds of binary, to be divided into integers and fractional parts, and finally the decimal point position. But to make sure that there are no mistakes in exams or that you need to practice regularly, it makes perfect.

In-C + + conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.