Implicit type conversion & negative Complement

Source: Internet
Author: User

Http://www.cppblog.com/suiaiguo/archive/2009/07/16/90228.html
Implicit type conversion and negative value completion code: a c ++ interview question: void Foo (void)
{
Unsigned int A = 6;
Int B =-20;
(A + B> 6 )? Puts ("> 6"): puts ("<= 6"); // puts is the print function
}

What is output? The answer is output> 6.
This question mainly describes two things.
1. implicit type conversion: int type variables are converted to unsigned int, and B to positive numbers.
2. complement of a negative number: the value in the computer system is represented (stored) in the form of a complement.

1. implicit type conversion in C ++

C ++ defines standard conversions between a set of built-in type objects, which are implicitly applied to objects by the compiler when necessary. The formula conversion ensures that binary operators, such as the two operands of addition or multiplication, are promoted to the same type and then represent the result type. Two general guiding principles are as follows:
1. To prevent loss of precision, if necessary, the type is always upgraded to a wider type.
2. All arithmetic expressions containing an ordered type less than the integer type will be converted to an integer before calculation.
As described above, these rules define a type conversion hierarchy, starting from the widest type long double, the other operand will be converted to Long Double regardless of its type. if two operands are not of the Long double type, if one of them is of the double type, the other is converted to the double type. For example:

Int ival;
Float fval;
Double dval;
Dval + fval + ival // both fval and ival are converted to double before calculation addition.

Similarly, if both operands are not of the double type and one of them is of the float type, the other is converted to the float type. For example:

Char cval;
Int ival;
Float fval;
Cval + ival + fval // before calculation addition, both ival and cval are converted to float

Otherwise, if both operands are not one of the three floating point types, they must be an integer type. Before determining a common target escalation type, the compiler will apply an integer escalation process to all integer types smaller than Int.
During integer increase, the types of char, signed Char, unsigned char, and short int are all upgraded to the types of Int. If the type space on the machine is sufficient to indicate all unsigned short values, this usually happens when short is represented by half a word while int is represented by one word, then, the unsigned short Int Is also converted to an int. Otherwise, it is promoted to an unsigned Int. The wchar_t and enumeration types are upgraded to the smallest integer type that can represent all the values of the underlying type. In the following expressions:

Char cval;
Bool found;
Enum mumber {M1, M2, M3} mval;
Unsigned long ulong;
Cval + ulong; ulong + found; mval + ulong;

Before determining the public type of the two operands to be elevated, both cval found and mval are promoted to the int type.
Once the whole value is upgraded and executed, the type comparison starts again. If an operand is of the unsigned long type, the second operand is also converted to the unsigned long type. In the preceding example, all three objects added to ulong are upgraded to the unsigned long type. If neither of the two operands is of the unsigned long type and one of the operands is of the long type, the other is also converted to the long type. For example:

Char cval;
Long lval;
Cval + 1024 + lval; // before addition, both cval and 1024 are upgraded to the long type.

There is an exception in the general conversion of the long type. If one operand is of the long type and the other is of the unsigned int type, only when the length of the long type on the machine is sufficient to accommodate all the values of the unsigned int (in general, in 32-bit operating systems, the long and INT types are expressed in a long way, so they do not meet the assumptions here.) unsigned int will be converted to the long type, otherwise, both operands are upgraded to the unsigned long type. If neither of the two operands is long and one of them is unsigned
Int type, the other is also converted to the unsigned int type. Otherwise, both operands must be int type.
Generally, the length relationships of various types are long double> float >=int >=short> char, unsigned> signed.
Although these arithmetic conversion rules may cause more confusion, The general idea is to keep the precision of the values involved in the type expression as much as possible. This is implemented by raising different types to the widest type currently displayed.

Ii. Negative Complement

In a computer system, all values are represented (stored) by supplementary codes ).
The main reason is that the complement code can be used to process the symbol bit and other digits in a unified manner. At the same time, the subtraction can also be processed by addition. In addition, when two numbers are added, if the highest bit (symbol bit) has an incoming bit, the carry is discarded.
The conversion process of the complement code is almost the same as that of the original code.
The value complement representation is also divided into two situations:
(1) positive complement: the same as the original code.
For example, the complement code of + 9 is 00001001.
(2) Complement of negative numbers: the sign bit is 1, and the rest is the original code of the absolute value of the number is reversed by bit; then the whole number is added to 1.
For example, the-7 complement: because it is a negative number, the symbol bit is "1", the whole is 10000111; the remaining 7 digits are the absolute values of-7 + the original code 0000111 of 7 is reversed to 1111000 by bit; plus 1, the complement code of-7 is 11111001.
If you know the complement of a number, you can perform the following operations on the source code:
(1) If the sign bit of the complement code is "0", it indicates a positive number. Therefore, the complement code is the original code of the number.
(2) If the sign bit of the complement code is "1", it indicates a negative number. The operation to evaluate the source code can be: the sign bit is 1, and the rest are reversed, and then the entire number is added to 1.
For example, if we know that a complement code is 11111001, the original code is 10000111 (-7): Because the symbol bit is "1", it indicates a negative number, so this bit remains unchanged and is still "1 "; the other 7 digits are reversed to 1111001, and then 1 is added, so it is 0000110.

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.