integer-promotion

Integer promotion - what are the steps

半城伤御伤魂 提交于 2019-11-29 06:52:19
This code prints B2 short a=-5; unsigned short b=-5u; if(a==b) printf("A1"); else printf("B2"); I read about integer promotion but it's still unclear to me, how does it work in the example here? Can someone thoroughly post the steps the compiler follows in widening/truncating the values? Let's walk through your code: short a = -5; a = -5, which fits into a short. So far so easy. unsigned short b = -5u; -5u means apply the unary - operator to the constant 5u. 5u is (unsigned int) 5, and the unary - does no promotion, so you end up with 4294967291 which is 2^32-5. (Update: I got this bit wrong

Integer promotion with the operator <<

此生再无相见时 提交于 2019-11-28 12:11:36
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 promoted left operand. If the value of the right operand is negative or is greater than or equal to the

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

守給你的承諾、 提交于 2019-11-28 10:46:44
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 compiler dependent. int a(1), b(2), c(3); double d(4.); double result1 = a + b/d + c; // equal to 4 or

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

牧云@^-^@ 提交于 2019-11-28 09:16:42
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 compliment of 1; the bit-pattern is evaluated as 255 (probably) thus 255 < 1 will evaluate to false

What is going on with bitwise operators and integer promotion?

℡╲_俬逩灬. 提交于 2019-11-28 01:51:19
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. I then used sizeof() and found... When I use the left shift bitwise operator( << ), I receive an

Integral promotion

纵然是瞬间 提交于 2019-11-28 01:05:41
问题 When is it ever the case that a signed integer cannot represent all the values of the original type with regards to integer promotion? From the text K&R, C Programming Language, 2nd Ed. p. 174 A.6.1 Integral Promotion A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer may be used. If an int can represent all the values of the original type, then the value is converted to int;

Why are integer types promoted during addition in C?

自闭症网瘾萝莉.ら 提交于 2019-11-28 00:38:53
So we had a field issue, and after days of debugging, narrowed down the problem to this particular bit of code, where the processing in a while loop wasn't happening : // heavily redacted code // numberA and numberB are both of uint16_t // Important stuff happens in that while loop while ( numberA + 1 == numberB ) { // some processing } This had run fine, until we hit the uint16 limit of 65535. Another bunch of print statements later, we discovered that numberA + 1 had a value of 65536 , while numberB wrapped back to 0 . This failed the check and no processing was done. This got me curious, so

Integer promotion - what are the steps

≯℡__Kan透↙ 提交于 2019-11-28 00:21:05
问题 This code prints B2 short a=-5; unsigned short b=-5u; if(a==b) printf("A1"); else printf("B2"); I read about integer promotion but it's still unclear to me, how does it work in the example here? Can someone thoroughly post the steps the compiler follows in widening/truncating the values? 回答1: Let's walk through your code: short a = -5; a = -5, which fits into a short. So far so easy. unsigned short b = -5u; -5u means apply the unary - operator to the constant 5u. 5u is (unsigned int) 5, and

Yoda Conditions and integer promotion

拜拜、爱过 提交于 2019-11-27 16:01:49
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 )? 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 operands to the == operator have arithmetic type, as in this case, then the "usual arithmetic conversions" are

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

人走茶凉 提交于 2019-11-27 09:48:50
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? This is due to integer promotions, both operands of the bit-wise shifts will be promoted to int in both cases. In the second case: x = (x << 1) >> 1; the result of x << 1 will be an int and therefore the