Questions tagged [integer-promotion]

Anything related to C and C++ integer promotions, i.e. a class of data-type conversions that happens automatically when an object of integer type appears in certain contexts (e.g. when a value of type `short` is added to an `int` it is automatically promoted to `int` type before performing the operation).

In C and C++ integer promotion refers to automatic type conversions made between compatible integral types. When an operation is attempted between two compatible types (one can be safely converted into the other) any necessary adjustments are added silently by the compiler. This helps to avoid type casting where the programmer's intent is clear.

193 questions
76
votes
4 answers

Why must a short be converted to an int before arithmetic operations in C and C++?

From the answers I got from this question, it appears that C++ inherited this requirement for conversion of short into int when performing arithmetic operations from C. May I pick your brains as to why this was introduced in C in the first place?…
dayuloli
  • 13,791
  • 13
  • 58
  • 103
73
votes
5 answers

Type conversion - unsigned to signed int/char

I tried the to execute the below program: #include int main() { signed char a = -5; unsigned char b = -5; int c = -5; unsigned int d = -5; if (a == b) printf("\r\n char is SAME!!!"); else …
user2522685
  • 860
  • 1
  • 7
  • 8
39
votes
3 answers

Why does it make a difference if left and right shift are used together in one expression or not?

I have the following code: unsigned char x = 255; printf("%x\n", x); // ff unsigned char tmp = x << 7; unsigned char y = tmp >> 7; printf("%x\n", y); // 1 unsigned char z = (x << 7) >> 7; printf("%x\n", z); // ff I would have expected y and z to…
32
votes
1 answer

Why is this function call ambiguous?

I'm reading the standard and trying to figure out why this code won't be resolved without a cast. void foo(char c) { } // Way bigger than char void foo(unsigned long int) { } int main() { foo(123456789); // ambiguous foo((unsigned long int)…
30
votes
2 answers

Bitshift and integer promotion?

Normally, C requires that a binary operator's operands are promoted to the type of the higher-ranking operand. This can be exploited to avoid filling code with verbose casts, for example: if (x-48U<10) ... y = x+0ULL << 40; etc. However, I've found…
R.. GitHub STOP HELPING ICE
  • 195,354
  • 31
  • 331
  • 669
28
votes
1 answer

Odd bit shifting behavior

I have the following C code which works: int ex(unsigned int x) { int mask = 0x55555555; int a = ((x >> 0) & mask ); return a + ((x >> 1) & mask ); } However, when I expand it to this, I get a different result: int ex(unsigned int x)…
cdignam
  • 1,158
  • 1
  • 11
  • 16
27
votes
3 answers

How do promotion rules work when the signedness on either side of a binary operator differ?

Consider the following programs: // http://ideone.com/4I0dT #include #include int main() { int max = std::numeric_limits::max(); unsigned int one = 1; unsigned int result = max + one; std::cout <<…
Billy ONeal
  • 97,781
  • 45
  • 291
  • 525
27
votes
4 answers

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

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…
Alan Turing
  • 11,403
  • 14
  • 66
  • 114
26
votes
4 answers

Why are integer types promoted during addition in C?

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…
peonicles
  • 1,357
  • 2
  • 16
  • 27
26
votes
2 answers

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

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…
goldenmean
  • 16,908
  • 50
  • 144
  • 206
25
votes
7 answers

What is wrong with this bit-manipulation code from an interview question?

I was having a look over this page: http://www.devbistro.com/tech-interview-questions/Cplusplus.jsp, and didn't understand this question: What’s potentially wrong with the following code? long value; //some stuff value &= 0xFFFF; Note: Hint to the…
Josh
  • 11,980
  • 9
  • 70
  • 116
24
votes
3 answers

How is shift operator evaluated in C?

I recently noticed a (weird) behavior when I conducted operations using shift >> <
chouaib
  • 2,714
  • 5
  • 18
  • 33
23
votes
5 answers

Why doesn't a negative number modulo a vector size give a negative number?

#include #include #include using namespace std; int main() { vector v = {1, 2, 3, 4, 5, 6, 7}; int i = -4; cout << i << endl; cout << v.size() << endl; cout << i % v.size() << endl; cout << -4 % 7 <<…
George Zhang
  • 347
  • 1
  • 3
22
votes
3 answers

How can I force the size of an int for debugging purposes?

I have two builds for a piece of software I'm developing, one for an embedded system where the size of an int is 16 bits, and another for testing on the desktop where the size of an int is 32 bits. I am using fixed width integer types from…
JDW
  • 440
  • 3
  • 11
22
votes
6 answers

Why is unsigned short (multiply) unsigned short converted to signed int?

Why is unsigned short * unsigned short converted to int in C++11? The int is too small to handle max values as demonstrated by this line of code. cout << USHRT_MAX * USHRT_MAX << endl; overflows on MinGW 4.9.2 -131071 because (source) USHRT_MAX =…
Slazer
  • 4,100
  • 7
  • 25
  • 53
1
2 3
12 13