C++primer Quick View Notes-type conversion

Source: Internet
Author: User

1. Type conversion
  
 
  1. bool b = 42; // _b is true_
  2. int i = b; // _i has value 1_
  3. i = 3.14; // _i has value 3_
  4. double pi = i; // _pi has value 3.0_
  5. unsigned char c = -1; // _assuming 8-bit chars_, _c has value 255_
  6. signed char c2 = 256; // _assuming 8-bit chars_, _the value of c2 is__undefined_

The range of values that a type can represent determines the process of conversion:

  • When we assign an arithmetic value of a non-Boolean type to a Boolean type, the initial value is 0 and the result is false,
    Otherwise, the result is true.
  • When we assign a Boolean value to a non-Boolean type, the initial value is false and the result is O, and the initial value is
    True, the result is 1.
  • When we assign a floating-point number to an integer type, the approximation is processed. The resulting value will retain only the floating-point
    The part before the decimal point.
  • When we assign an integer value to a floating-point type, the fractional part is recorded as 0. If the integer occupies a space that is super-
    There may be a loss of precision over the capacity of floating-point types.
  • When we assign an unsigned type to a value that exceeds the range it represents, the result is an initial value pair of unsigned type table
    Shows the remainder of the total number of values after modulo. For example, a 8-bit-size unsigned char can represent a value within a range of 0 to 255, and if we assign a value other than an interval, the actual result is the remainder of the value to 256 modulo (negative numbers plus the modulo of the unsigned number, where modulo = x 2 )。 Therefore, the result of assigning 1 to a 8-bit size unsigned char is 255.

    In the computer system, the values are all used in the complement to represent and store, positive integer complement is the second binary representation, the same as the original code, the complement of the negative integer, the corresponding positive binary representation of all the bit inversion (including the sign bit, 0 change to 0) plus 1. -1 of the original code 10000001 is converted to complement: 11111111, so it is considered as an unsigned positive integer with a value of 255.

  • When we assign a signed type to a value that exceeds its range, the result is undefined (undefined).
    At this point the engineering program may continue to work, may crash, and may generate garbage data.
    Information Supplement :
    Automatic type conversion in arithmetic operations
    Basic principle: A type with a narrower range is converted to a wide range of domains

    Also pay attention to the following 3 points:
  1. Signed and unsigned characters in the expression ( for characters ) and short integers are all converted to integers, and if the int type can represent a value of the original type, it is converted to an int type; otherwise it is converted to the unsigned type.
  2. When one operand is of type long and the other is a unsigned type, if long can represent the full value of unsigned, the unsigned is converted to long; otherwise, the two operands are converted to unsigned long.
  3. float and another operand are no longer converted to a double type when the wide range of two operands is of type float
    We need to be awarearithmetic problem between signed number and unsigned number
    When there are signed and unsigned types in an expression, all operands are automatically converted tounsigned type。 Therefore, in this sense, the operation priority of an unsigned number is higher than the signed number.
    The following experiment from the Netizen, I in the VS2013 platform to verify:

    First, an experiment was made to define a signed int data and a unsigned int data, and then compare the size:
    unsigned int a=20;
    signed int b=-130;
    A>b, or B>a? The experiment proves that b>a, that is to say, -130>20, why do such results occur?
    This is because in C language operations, if you encounter an unsigned number and signed number of operations, the compiler will automatically convert to unsigned number for processing, so a=20,b=4294967166, so the comparison goes on of course b>a.
    One more example:
    unsigned int a=20;
    signed int b=-130;
    std::cout<<a+b<<std::endl;
    The result output is 4294967186, the same reason, before the operation, A=20,b is converted to 4294967166, so a+b=4294967186
    Subtraction and multiplication result in a similar operation.
    If the type of B is not affected by the operation between B=-130,b and immediate number as signed int data, the result of the operation is still signed int:
    signed int b=-130;
    std::cout<<b+30<<std::endl;
    The output is-100.
    For floating-point numbers, floating-point numbers (float,double) are actually signed, and the unsigned and signed prefixes cannot be added to the float and double, and there is no question of the conversion between the signed number root unsigned numbers.

    Such a question, is said to be Microsoft interview question:
    unsigned int i=3;
    Cout<<i -1;
    Ask what the result is.
    First reaction:-3. But the result does not seem like this, wrote a program, ran a bit, found that: 4294967293.
    On 32-bit machines, both int and unsigned int are 32-bit (4 bytes), in the expression I
    -1, i is unsigned int, 1 is an int (the type of the constant integer is the same as the enum), According to 5th can know 1 must be converted to unsigned int, namely 0xFFFFFFFF, Decimal 4294967295, and then multiply with I, namely 4294967295*3, if overflow is not considered, the result is 12884901885, Hexadecimal 0x2fffffffd, since unsigned int can only represent 32 bits, ignoring high four bits, the result is 0xfffffffd, or 4294967293.



From for notes (Wiz)

C++primer Quick View Notes-type 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.