面试题:
unsigned int a = 6;
int b = -20;
int c;
(a+b>6)?(c=1):(c=0)
问:c=?
看下面这段:
K & R A.6.5 Arithmetic Conversions
First, if either operand is long double, the other is converted to long double.
Otherwise, if either operand is double, the other is converted to double.
Otherwise, if either operand is float, the other is converted to float.
Otherwise, the integral promotions are performed on both operands;
then, if either operand is unsigned long int, the other is converted to unsigned long int.
Otherwise, if one operand is long int and the other is unsigned int, the effect depends on whether a long int can represent all values of an unsigned int;
if so, the unsigned int operand is converted to long int; if not, both are converted to unsigned long int.
Otherwise, if one operand is long int, the other is converted to long int.
Otherwise, if either operand is unsigned int, the other is converted to unsigned int.
Otherwise, both operands have type int.
简单翻译如下:
如果任一个操作数是long double, 则另一个要转换为long double
如果任一个操作数是double, 则另一个要转换为double
如果任一个操作数是float, 则另一个要转换为float
此外整数运算符升级对两个操作数都有影响;
如果任一个操作数是unsigned long int, 则另一个要转换为unsigned long int
如果一个操作数是long int, 另一个是unsigned int,
如果long int可以表示结果,则unsigned int要转换为long int;否则两个都转换为unsigned long int
如果任一个操作数是long int, 则另一个要转换为long int
如果任一个操作数是unsigned int, 则另一个要转换为unsigned int
除此之外,两个操作数都应是int
倒数第二句是解本题的关键:
如果任一个操作数是unsigned int, 则另一个要转换为unsigned int
首先将b=-20用补码表示:20的二进制:0000 0000 0001 0100
取反:1111 1111 1110 1011
再加1:1111 1111 1110 1100
把第一个代表负号的1去掉,0111 1111 1110 1100
把上面作为unsigned int 是十进制32748财(a+b)肯定大于6,则c=1
unsigned int a = 6;
int b = -20;
int c;
(a+b>6)?(c=1):(c=0)
问:c=?
看下面这段:
K & R A.6.5 Arithmetic Conversions
First, if either operand is long double, the other is converted to long double.
Otherwise, if either operand is double, the other is converted to double.
Otherwise, if either operand is float, the other is converted to float.
Otherwise, the integral promotions are performed on both operands;
then, if either operand is unsigned long int, the other is converted to unsigned long int.
Otherwise, if one operand is long int and the other is unsigned int, the effect depends on whether a long int can represent all values of an unsigned int;
if so, the unsigned int operand is converted to long int; if not, both are converted to unsigned long int.
Otherwise, if one operand is long int, the other is converted to long int.
Otherwise, if either operand is unsigned int, the other is converted to unsigned int.
Otherwise, both operands have type int.
简单翻译如下:
如果任一个操作数是long double, 则另一个要转换为long double
如果任一个操作数是double, 则另一个要转换为double
如果任一个操作数是float, 则另一个要转换为float
此外整数运算符升级对两个操作数都有影响;
如果任一个操作数是unsigned long int, 则另一个要转换为unsigned long int
如果一个操作数是long int, 另一个是unsigned int,
如果long int可以表示结果,则unsigned int要转换为long int;否则两个都转换为unsigned long int
如果任一个操作数是long int, 则另一个要转换为long int
如果任一个操作数是unsigned int, 则另一个要转换为unsigned int
除此之外,两个操作数都应是int
倒数第二句是解本题的关键:
如果任一个操作数是unsigned int, 则另一个要转换为unsigned int
首先将b=-20用补码表示:20的二进制:0000 0000 0001 0100
取反:1111 1111 1110 1011
再加1:1111 1111 1110 1100
把第一个代表负号的1去掉,0111 1111 1110 1100
把上面作为unsigned int 是十进制32748财(a+b)肯定大于6,则c=1