integer-promotion

Is unsigned char always promoted to int?

心已入冬 提交于 2019-11-27 07:44:52
问题 Suppose the following: unsigned char foo = 3; unsigned char bar = 5; unsigned int shmoo = foo + bar; Are foo and bar values guaranteed to be promoted to int values for the evaluation of the expression foo + bar -- or are implementations allowed to promote them to unsigned int ? In section 6.2.5 paragraph 8: For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange

Does one double promote every int in the equation to double?

老子叫甜甜 提交于 2019-11-27 03:48:00
问题 Does the presence of one floating-point data type (e.g. double ) ensure that all +, -, *, /, %, etc math operations assume double operands? If the story is more complicated than that, is there a resource that describes these rules? Should I not ask such questions and always explicitly cast int to double when the result of the equation is double . Here are some equations I'm thinking about. I purposefully did not compile and run then on my system, since this is the type of thing that could be

Data type promotions during arithmetic operations: -1 < (unsinged int) 1 == false

假如想象 提交于 2019-11-27 02:48:04
问题 main() { if ( -1 < (unsigned char) 1 ) printf("less than"); else printf("NOT less than"); } Prints less than . Because, (unsigned char) 1 is converted to (signed char) 1 and then: (signed) -1 < (signed) 1 , thus output is less than . But if I change the above code to if ( (-1 < (unsigned int) 1 ) then the output is NOT less than . So it's obvious that when I change unsigned char to unsigned int: (signed) -1 is converted to unsigned int [exactly opposite is happening] since -1 is stored as 2's

What is going on with bitwise operators and integer promotion?

不羁岁月 提交于 2019-11-26 22:01:20
问题 I have a simple program. Notice that I use an unsigned fixed-width integer 1 byte in size. #include <cstdint> #include <iostream> #include <limits> int main() { uint8_t x = 12; std::cout << (x << 1) << '\n'; std::cout << ~x; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.get(); return 0; } My output is the following. 24 -13 I tested larger numbers and operator << always gives me positive numbers, while operator ~ always gives me negative numbers

Integer promotion with the operator <<

三世轮回 提交于 2019-11-26 21:49:18
问题 Similar to the question Bitshift and integer promotion?, I have a question about integer promotion when using left bitshifts. unsigned int test(void) { unsigned char value8; unsigned int result; value8 = 0x12; result = value8 << 8; return result; } In this case, will be the value8 first promote to unsiged int or is it implementation specific? 6.5.7 Bitwise shift operators ... 3 Sematics ... The integer promotions are performed on each of the operands. The type of the result is that of the

Addition of two chars produces int

眉间皱痕 提交于 2019-11-26 18:58:28
I've made a simple program and compiled it with GCC 4.4/4.5 as follows: int main () { char u = 10; char x = 'x'; char i = u + x; return 0; } g++ -c -Wconversion a.cpp And I've got the following: a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to ‘char’ from ‘int’ may alter its value The same warning I've got for the following code: unsigned short u = 10; unsigned short x = 0; unsigned short i = u + x; a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to ‘short unsigned int’ from ‘int’ may alter its value Could anyone please explain me why addition of two chars

Are the “usual arithmetic conversions” and the “integer promotions” the same thing?

不羁的心 提交于 2019-11-26 17:23:38
问题 Are the "usual arithmetic conversions" and the "integer promotions" the same thing? I have read that the "usual arithmetic conversions" are used to make the operands of an expression the same type, while "integer promotions" are used to promote the types smaller than int to int , but in MSDN both of these concepts are placed under "usual arithmetic conversions" only. 回答1: No. The usual arithmetic conversions involve integral promotion under certain circumstances, but these are two separate

Yoda Conditions and integer promotion

我只是一个虾纸丫 提交于 2019-11-26 17:23:17
问题 When comparing a type larger than int , with an integer constant, should I place the constant on the left or the right to ensure the correct comparison is performed? int64_t i = some_val; if (i == -1) or should it be: if (-1 == i) Are there any circumstances in which either case is not identical to comparison with -1LL (where int64_t is long long )? 回答1: It doesn't matter whether you put it on the right hand side or the left hand side; the == operator is completely symmetrical. If both

Why does combining two shifts of a uint8_t produce a different result?

拜拜、爱过 提交于 2019-11-26 14:51:59
问题 Could someone explain me why: x = x << 1; x = x >> 1; and: x = (x << 1) >> 1; produce different answers in C? x is a *uint8_t* type (unsigned 1-byte long integer). For example when I pass it 128 (10000000) in the first case it returns 0 (as expected most significant bit falls out) but in the second case it returns the original 128 . Why is that? I'd expect these expressions to be equivalent? 回答1: This is due to integer promotions, both operands of the bit-wise shifts will be promoted to int

In a C expression where unsigned int and signed int are present, which type will be promoted to what type?

蓝咒 提交于 2019-11-26 10:30:16
I have a query about data type promotion rules in C language standard. The C99 says that: C integer promotions also require that "if an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int." My questions is in case of a C language expression where unsigned int and signed int are present, which type will be promoted to what type? E.g. int cannot represent all the values of the unsigned int (values larger than MAX_INT values) whereas unsigned int cannot represent the -ve values, so what type is promoted to what in such