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.

239 questions
0
votes
1 answer

integer promotion of bit-wise operation and endiannes issue

Consider the following snippet from code running in the Linux kernel: char *d; u32 mask, step, val; ... /* d is initialized with valid pointer pointing at buffer, and mask, step and val are initialized to some sane values as well.…
Mark
  • 6,052
  • 8
  • 61
  • 129
0
votes
0 answers

Are ints promoted to long long when multiplied

In the following code, is product guaranteed to contain a correct answer: int num=INT_MAX, num2=INT_MAX; long long product = num1 * num2;
Natan Yellin
  • 6,063
  • 5
  • 38
  • 57
0
votes
1 answer

When does type conversion happen in C

I have following question: e.g. I have given code: uint8_t a = 0x30; uint16_t b = a << 8; Will a be first shifted and then converted to uint16_t? Or will it be converted to uint16_t first? Or is this behavior compiler dependent? I'm trying to…
Zhani Baramidze
  • 1,407
  • 1
  • 13
  • 32
0
votes
2 answers

C++ negation and overload resolution

In Microsoft Visual Studio 2015, the following code: void foo(int8_t a); void foo(int16_t a); void foo(int16_t a, int16_t b); void f() { int8_t x /* = some value */; foo(-int16_t(x)); // ERROR } Gives the following message: foo Error:…
Isaac
  • 816
  • 5
  • 12
0
votes
1 answer

Understanding the order of conversions, arithmetic conversions, and integer promotions for non-overloaded bitwise operators

I want to understand exactly what is happening, when the compiler encounter a non overloaded operator and what conversions are operated. As an example, let's take the bitwise operators, and for example &. The standard says : [expr.bit.and] The…
Vincent
  • 57,703
  • 61
  • 205
  • 388
0
votes
2 answers

unsigned short integer promotion in 32-bit system

I have a 32-bit integer size if I have an arithmetic expression like unsigned short current_time, last_time if((current_time - last_time) > timeout) I believe current_time and last_time will both be converted to signed int 32 before the…
gogolf0401
  • 73
  • 1
  • 10
0
votes
2 answers

Conversion of big-endian long in C++?

I need a C++ function that returns the value of four consecutive bytes interpreted as a bigendian long. A pointer to the first byte should be updated to point after the last. I have tried the following code: inline int32_t bigendianlong(unsigned…
PAF
  • 23
  • 4
0
votes
0 answers

Integer promotion with hex constants

I have some code here: #include int main () { char foo = 0xE7; if (foo == 0xE7) printf ("true\n"); else printf ("false\n"); return 0; } That prints "false". I'm not too concerned about that because I can believe that…
Nick Gammon
  • 1,173
  • 10
  • 22
0
votes
1 answer

Integer promotions, value bits, and multiplying

If we multiply two uint32_t types and they type inton this system has 63 value bits and one sign bit, then those values are converted to int( integer promotions ), multiplied, and converted back to uint32_t. The intermediate result cannot be…
0
votes
1 answer

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

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
Duggs
  • 169
  • 1
  • 12
0
votes
1 answer

Why no promotion when adding ints to List

in Java I created an ArrayList of Double and I invoked the method list.add(1), however, I get an error. If I can assign an int to a double variable like this: double num = 1; due to automatic promotion, then why can't I add a 1 to ArrayList of…
Thuy
  • 1,493
  • 1
  • 11
  • 11
0
votes
3 answers

Compiler warnings conversion

We are compiling using gcc with -Wconversion enabled. I get following warnings when I left shift result returned by isBitSet function below. warning: conversion to 'u_int16_t {aka short unsigned int}' from 'int' may alter its value…
Sirish
  • 9,183
  • 22
  • 72
  • 107
0
votes
1 answer

Is the promotion to unsigned done on the result or on each operand?

In the following: 2147483647U > -2147483647 - 1 it will evaluate to false because of the conversion/promotion to unsigned. My question is though how it will be promoted? Will the subtraction operation be done first and the result will be promoted to…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
0
votes
1 answer

Can you please explain this—"the rules involving mixed types of operands do not apply to the shift operators"

I can't understand the line "The result of the shift has the same type as the thing that got shifted (after the integral promotions)" in the following extract from the C book by Mike Banahan (Section 2.8.2.3). Importantly, the rules involving…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
0
votes
4 answers

How possibly can a unary +/- operator cause integral promotion in "-a" or "+a", 'a' being an arithmetic data type constant/variable?

This seemingly trivial line is taken from the C book my Mike Banahan & Brady (Section 2.8.8.2). I can understand how implicit promotion comes into play in expressions like c=a+b depending on the types of the operands, but I am unable to grasp how…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
1 2 3
15
16