Review of C++ program classification - hexadecimal conversion and representation of floating point numbers

type of integer constant

An integer constant can be represented in 3 different ways:

(1) Decimal integer . The decimal system is composed of nine digits 0-9, and cannot start with 0. Such as l314, -432, 0, etc. Adding a letter l or L after an integer constant is considered a long int constant. Such as 123L, 421L, OL, etc., which are often used in function calls. If the formal parameter of the function is long int, the actual parameter is also required to be long int. At this time, it is not possible to use 123 as the actual parameter, but 123L as the actual parameter.

(2) Octal integers . Composed of 0-7 numbers, adding a number 0 at the beginning of the constant means that this is a constant expressed in octal number form. For example, 020 means that this is the octal number 20, which is equivalent to the decimal number 16.

(3) Hexadecimal integer . Consists of 0-9 and AF (A stands for 10 in decimal, and so on, F stands for 15). Adding a number 0 and an English letter X (or x) at the beginning of the constant indicates that this is a constant expressed in hexadecimal number form. For example, 0X20 means that this is the hexadecimal number 20, which is equivalent to the decimal number 32.

type conversion

  • Decimal Integer to Non-Decimal Integer

Method: divide the base and get the remainder (the base is divided and the remainder is taken, the first remainder is low (bit), and the last remainder is high (bit))

Example: 55L to 110111 (binary)

Dividing the base and taking the remainder is to divide the converted number by the base number to be converted, here it is converted to binary, so use 55 to divide by 2, and record the remainder obtained after each division.

        55%2为1,实则商为27余数为1,继续用27除以2,记录余数,直至最后的商为0结束。

        55%2=1    27%2=1    13%2=1    6%2=0     3%2=1    1%2=1(商为0,结束)

"The first remainder is low (bit), and the last remainder is high (bit)", that is, the remainder obtained first is arranged in the last place, that is, the remainder obtained is arranged backwards, and finally 110111 is obtained.

  • Decimal decimal conversion to non-decimal decimal

Method: Multiply base rounding method (multiply base rounding, first round to high (bit), and then round to low (bit))

Example: 0.625L

Multiply base rounding is to multiply the converted number by the base number to be converted, here it is converted to binary, so multiply 0.625 by 2, record the integer part obtained by each multiplication base, and then use the obtained decimal part to continue multiplying Base until the decimal place is 0 to end.

Multiply 0.625 by 2 to get 1.25, record integer part 1, and so on until the decimal part is 0.

             0.625 * 2 = 1.25    0.25 * 2 = 0.5    0.5 * 2 = 1.0

Therefore, the final result is 101. Note that "the first integer is high, and the second is low". This is the opposite of the integer part. The first obtained is the high part, which is ranked first.

  • Converting a non-decimal integer to a decimal integer

Law: The number at the corresponding position is multiplied by the n-1 power of the base system, and then accumulated.

Example: convert binary integer 101 to decimal

              1 * 2²  +  0 * 2¹  +  1 * 2º=5   所以二进制101转换为十进制为5。
  • Convert binary integer to octal integer

With the decimal point as the boundary, the integer part from right to left and the decimal part from left to right are divided into groups of three digits (if less than three digits are filled with 0), and then each three-digit binary number is converted into a corresponding octal number.

Example: binary number: 10111001010.1011011

       首先按原则划分为    010   111   001   010.101   101   100
                                         
       然后对位转换         2     7     1     2 . 5     5     4
  • Binary integer to hexadecimal integer

With the decimal point as the boundary, the integer part from right to left and the decimal part from left to right are divided into groups of four (if less than four digits are filled with 0), and then each four-digit binary number is converted into the corresponding hexadecimal system of numbers.

Example: binary number: 10111001010.1011011

        首先按原则划分为    0101   1100   1010 . 1011   0110

        然后对位转换         5      C      A   .  B      6
  • Convert an octal integer to a binary integer

Each octal number is converted to the corresponding three-digit binary number, and 0 can be added if it is insufficient.

Example: 0357 (octal number 357)

               3转为011      5转为101      7转为111       即011101111
  • hexadecimal integer to binary integer

Like octal, each hexadecimal integer is converted to the corresponding four-digit binary number, and 0 can be added for insufficient numbers.

Example: A6F

                A 转为1010       6 转为0110       F 转为1111       即101001101111
  • Conversion between octal and hexadecimal

The conversion between the two can be done by means of decimal or binary, and the octal can be converted to decimal or binary first, and then converted to hexadecimal. It does so through indirect conversion.

Representation of floating point numbers

A floating point number can be represented in two different ways:

(1) Decimal form . Such as 21.456, -7.98, etc. It generally consists of an integer part and a fractional part, one of which can be omitted (such as 78. or .06, .0), but not both. The C++ compilation system treats floating-point numbers expressed in this form as double-precision constants, which occupy 8 bytes in memory. If the letter F or f is added after the number of the real number, it means that the number is a single-precision floating-point number , such as 1234F, -43f, which occupies 4 bytes. If you add the letter L or 1, it means that the number is a long double precision number (long double), which occupies 12 bytes in GCC and 8 bytes in Visual C++.

(2) Exponential form (that is, floating-point form) . A floating point number can be written in exponential form such as 3.14159. In the program, it is expressed as 0.314159e1, 3.14159e0, 31.4159e-1, 314.159e-2, and the letter e is used to indicate that the subsequent number is a power of base 10, such as e12 means 10 to the 12th power.

Guess you like

Origin blog.csdn.net/Terminal_ve/article/details/126131342