integer-promotion

Why isn't common_type<long, unsigned long>::type = long long?

会有一股神秘感。 提交于 2019-12-03 09:52:49
common_type<long, unsigned long>::type is unsigned long because concerning the operands after integral promotion the standard says... [...] if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type Not to call the integral promotion system buggy, but it seems like if there is a bigger signed integer type which can can represent the range of both signed and unsigned operands it should be used. I know some platforms might

In java why prefix increment or decrement operator does not require cast in case of byte

风格不统一 提交于 2019-12-02 22:31:53
问题 In java Suppose i have following code snippet byte b = 127; b=-b ;//(which require a cast due to numeric promotion) b=++b; //does not require cast 回答1: The JLS specification of ++ says: The type of the prefix increment expression is the type of the variable. .... Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (

Type of char multiply by another char

萝らか妹 提交于 2019-12-02 04:46:31
What is the type of the result of a multiplication of two chars in C/C++? unsigned char a = 70; unsigned char b = 58; cout << a*b << endl; // prints 4060, means no overflow cout << (unsigned int)(unsigned char)(a*b) << endl; // prints 220, means overflow I expect the result of multiplying two number of type T (e.g., char, short, int) becomes T. It seems it is int for char because sizeof(a*b) is 4. I wrote a simple function to check the size of the result of the multiplication: template<class T> void print_sizeof_mult(){ T a; T b; cout << sizeof(a*b) << endl; } print_sizeof_mult<char>() , print

Integral promotion and operator+=

*爱你&永不变心* 提交于 2019-12-01 16:29:52
I need to eliminate gcc -Wconversion warnings. For example typedef unsigned short uint16_t; uint16_t a = 1; uint16_t b = 2; b += a; gives warning: conversion to 'uint16_t {aka short unsigned int}' from 'int' may alter its value [-Wconversion] b += a; ~~^~~~ I can eliminate this by uint16_t a = 1; uint16_t b = 2; b = static_cast<uint16_t>(b + a); Is there any way to keep the operator+= and eliminate the warning? Thank you. EDIT I use gcc test.cpp -Wconversion my gcc version is gcc.exe (Rev3, Built by MSYS2 project) 7.2.0 I need to eliminate gcc -Wconversion warnings. You don't say why but this

Integral promotion and operator+=

a 夏天 提交于 2019-12-01 15:54:33
问题 I need to eliminate gcc -Wconversion warnings. For example typedef unsigned short uint16_t; uint16_t a = 1; uint16_t b = 2; b += a; gives warning: conversion to 'uint16_t {aka short unsigned int}' from 'int' may alter its value [-Wconversion] b += a; ~~^~~~ I can eliminate this by uint16_t a = 1; uint16_t b = 2; b = static_cast<uint16_t>(b + a); Is there any way to keep the operator+= and eliminate the warning? Thank you. EDIT I use gcc test.cpp -Wconversion my gcc version is gcc.exe (Rev3,

integer promotion in c

允我心安 提交于 2019-12-01 12:27:14
Let say I have a 32-bit machine. I know during integer promotion the expressions are converted to:\ int if all values of the original type can be represented in int unsigned otherwise Could you please explain what will happen for the following expression? and In general, how ranking works here? First snippet: si16 x, pt; si32 speed; u16 length; x = (speed*pt)/length; Second one: x = pt + length; EDIT: si16 means signed short (size 16 bit), si32 bit means signed int (size 32 bit) and u16 means unsigned short (size 16) I found the following link that has described the issue very clearly:

integer promotion in c

时间秒杀一切 提交于 2019-12-01 10:08:01
问题 Let say I have a 32-bit machine. I know during integer promotion the expressions are converted to:\ int if all values of the original type can be represented in int unsigned otherwise Could you please explain what will happen for the following expression? and In general, how ranking works here? First snippet: si16 x, pt; si32 speed; u16 length; x = (speed*pt)/length; Second one: x = pt + length; EDIT: si16 means signed short (size 16 bit), si32 bit means signed int (size 32 bit) and u16 means

Which integral promotions do take place when printing a char?

限于喜欢 提交于 2019-11-30 23:40:29
问题 I recently read that unsigned char x=1; printf("%u",x); invokes undefined behaviour since due to the format specifier %u, printf expects an unsigned int. But still I would like to understand what is going on in this example. I think that the integral promotion rules apply with the expression printf("%u",x) and the value represented by x . 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

MISRA C:2004, error with bit shifting

情到浓时终转凉″ 提交于 2019-11-30 21:54:37
I'm using IAR Workbench compiler with MISRA C:2004 checking on. The fragment is: #define UNS_32 unsigned int UNS_32 arg = 3U; UNS_32 converted_arg = (UNS_32) arg; /* Error line --> */ UNS_32 irq_source = (UNS_32)(1U << converted_arg); The MISRA error is: Error[Pm136]: illegal explicit conversion from underlying MISRA type "unsigned char" to "unsigned int" (MISRA C 2004 rule 10.3) I don't see any unsigned char in any of the code above. The discussion at Why did Misra throw an error here? discusses multiplication which may have different promoting rules than left shifting. My understanding is

type promotion in C

偶尔善良 提交于 2019-11-30 20:30:57
I am quite confused by the following code: #include <stdio.h> #include <stdint.h> int main(int argc, char ** argv) { uint16_t a = 413; uint16_t b = 64948; fprintf(stdout, "%u\n", (a - b)); fprintf(stdout, "%u\n", ((uint16_t) (a - b))); return 0; } That returns: $ gcc -Wall test.c -o test $ ./test 4294902761 1001 $ It seems that expression (a - b) has type uint32_t. I don't uderstand why since both operators are uint16_t. Can anyone explain this to me? The C standard explains this quite clearly (§6.5.6 Additive Operators): If both operands have arithmetic type, the usual arithmetic conversions