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
0
votes
1 answer

enum class - printf prints wrong value

I have this enum class: enum class myEnum : u8{ LEVEL_ERROR = 0, LEVEL_WARN = 50, LEVEL_DEBUG = 150, }; and at some point I making use of it (not exactly this way, but simply this is what happens): myEnum instance = 42; printf("My…
Michał
  • 529
  • 1
  • 3
  • 17
0
votes
0 answers

Are there any active proposals to change integral promotion rules?

As already discussed here integral promotion rules can cause headaches in some cases (especially for arithmetic operations between signed and unsigned types). At the end of this answer there is a link to a proposal. However that proposal is from…
Timo
  • 7,388
  • 2
  • 18
  • 44
0
votes
1 answer

Comparing results after integer promotion

Following situation: I have macros for storing the complement of a variable together with its original value within a structure. With another macro I want to check if the original value is equal to the complement of the stored complement value. But…
mbed_dev
  • 1,290
  • 14
  • 31
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
  • 4,824
  • 5
  • 38
  • 85
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
  • 5,265
  • 4
  • 30
  • 55
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,248
  • 11
  • 29
0
votes
1 answer

C++ Arithmetic Type Conversion with unsigned & signed integers

Code: #include using std::cout; using std::endl; int main() { unsigned int i = 5; int x = -3; cout << "(i + x) = " << (i + x) << endl; cout << "Set x to -6" << endl; x = -6; cout << "(i + x) = " << (i + x) <<…
Feez
  • 33
  • 4
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
  • 796
  • 4
  • 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
  • 50,257
  • 51
  • 171
  • 339
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
  • 63
  • 1
  • 7
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
  • 3
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,025
  • 10
  • 19
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
  • 179
  • 1
  • 11
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,005
  • 8
  • 6
1 2 3
12
13