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
3
votes
2 answers

integer promotion in c

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…
Monir
  • 1,402
  • 14
  • 16
3
votes
0 answers

type of an unsigned short subtraction

i have a short program, that is not compiling: #include int main(int argc, char *argv[]) { unsigned short a = 490; unsigned short b = 43; unsigned short d = std::min(b, a-b); return 0; …
Adam
  • 488
  • 4
  • 17
3
votes
2 answers

Ambiguous call to overloaded integer constructor

I shall provide these two constructors. BigUnsigned(int value) :BigUnsigned(static_cast(value)){ } BigUnsigned(unsigned long long value); The problem is the call is ambiguous for some argument values. According to this…
Slazer
  • 4,750
  • 7
  • 33
  • 60
3
votes
5 answers

Bitwise or before casting to int32

I was messing about with arrays and noticed this. EG: int32_t array[]; int16_t value = -4000; When I tried to write the value into the top and bottom half of the int32 array value, array[0] = (value << 16) | value; the compiler would cast the…
3
votes
2 answers

Arithmetic conversion VS integral promotion

char cval; short sval; long lval; sval + cval; // sval and cval promoted to int cval + lval; // cval converted to long This is a piece of code on C++ Primer. I know sval+cval generates an int type according to convert the small integral types to…
Des1gnWizard
  • 477
  • 1
  • 4
  • 13
3
votes
4 answers

Will char and short be promoted to int before being demoted in assignment expressions?

After doing some research I know in arithmetic expressions char and short will be promoted to int internally. But I am still wondering whether integer promotions like that will occur in assignment internally. (So please don't give me links only…
Mensu
  • 33
  • 3
3
votes
1 answer

Why do the upper 32 bits of a uint64_t become one whilst performing a specific bitwise operation?

Can someone please explain to me why the upper 32 bits of a uint64_t are set to one in case number #2: uint64_t ret = 0; ret = (((uint64_t)0x00000000000000FF) << 24); printf("#1 [%016llX]\n", ret); ret = (0x00000000000000FF << 24); printf("#2…
3
votes
2 answers

Left shifts and right shifts on boolean

I am trying to understand exaclty how integral promotion works with arithmetic shifts operators. Particularly, I would like to know, which values of a, b, c, d, e, f, g, h are exactly defined according to the C++14 standard, and which ones can…
Vincent
  • 57,703
  • 61
  • 205
  • 388
3
votes
1 answer

In the expression bool << integer, is bool promoted to int, or to the same type as the integer?

Consider the following code: // E1 << E2 with E1 bool, and E2 an integer true << static_cast(1); true << static_cast(1); true << static_cast(1); true << static_cast(1); true << static_cast(1); true…
Vincent
  • 57,703
  • 61
  • 205
  • 388
3
votes
1 answer

Is unary minus equivalent to binop minus?

My C compiler gave a warning when using unary minus on an unsigned value, so I fixed the warning by doing a subtraction from 0 instead. Now I wonder if the current code is equivalent to the original one: uint32_t a, b; // assume b is initialized and…
Log
  • 239
  • 1
  • 5
3
votes
1 answer

argument promotions in C function calls

I learned from ----As to when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when there's no prototype or when the argument is variadic. But an example…
HaoCheng
  • 135
  • 1
  • 4
3
votes
1 answer

Using unsigned int instead of unsigned short changes behaviour

I am attempting the htoi(char*) function from The C Programming Language by K&R (Excercise 2-3, pg. 43). The function is meant to convert a hexadecimal string to base 10. I believe I have it working. This is my code: #include #include…
aanrv
  • 2,159
  • 5
  • 25
  • 37
3
votes
1 answer

Are the "usual arithmetic conversions" and the "integer promotions" the same thing?

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…
John
  • 1,049
  • 1
  • 14
  • 34
3
votes
2 answers

Integral promotion/conversion: why should I care about the name of the resulting type?

I have been trying to wrap my head around the C99 rules of integral promotion and usual arithmetic conversions of integral types. After burning a few neurons, I came out with a set of rules of my own, which are a lot simpler and yet, I believe,…
Edgar Bonet
  • 3,416
  • 15
  • 18
3
votes
3 answers

bit representation of unsigned int a = -1

What is the bit representation of unsigned int x =-1; Can we assign unsigned int with a negative integer? #include int main(){ unsigned int x = -1; int y = ~0; if(x == y) printf("same"); else …
pranav prashant
  • 307
  • 1
  • 3
  • 13