Skip to main content

No, this is not a bug. You are using a compile time constant expression which is signed int unless told otherwise. Therefore you can only represent numbers between -32768 and 32767. Your computation of 255 * 255 = 65025 exceeds the range. Thus you see a wrap aroundan overflow. In the C/C++ standard the overflow of signed types is actually undefined behavior. That means that the compiler is allowed to do anything from showing the correct answer to halt and catch fire. Only unsigned types with a known size like uint16_t have a well-deineddefined overflow behaviour. You should promote your right hand side computation to unsigned type like this:

z = 255U * 255U;

This way you tell the compiler your intention and don't land in "Undefined-Behaviour-Land".

No, this is not a bug. You are using a compile time constant expression which is signed int unless told otherwise. Therefore you can only represent numbers between -32768 and 32767. Your computation of 255 * 255 = 65025 exceeds the range. Thus you see a wrap around. In the C/C++ standard the overflow of signed types is actually undefined behavior. That means that the compiler is allowed to do anything from showing the correct answer to halt and catch fire. Only unsigned types with a known size like uint16_t have a well-deined overflow behaviour. You should promote your right hand side computation to unsigned type like this:

z = 255U * 255U;

This way you tell the compiler your intention and don't land in "Undefined-Behaviour-Land".

No, this is not a bug. You are using a compile time constant expression which is signed int unless told otherwise. Therefore you can only represent numbers between -32768 and 32767. Your computation of 255 * 255 = 65025 exceeds the range. Thus you see an overflow. In the C/C++ standard the overflow of signed types is actually undefined behavior. That means that the compiler is allowed to do anything from showing the correct answer to halt and catch fire. Only unsigned types with a known size like uint16_t have a well-defined overflow behaviour. You should promote your right hand side computation to unsigned type like this:

z = 255U * 255U;

This way you tell the compiler your intention and don't land in "Undefined-Behaviour-Land".

deleted 299 characters in body
Source Link
Kwasmich
  • 1.5k
  • 12
  • 18

No, this is not a bug. You are using a compile time constant expression which is longsigned int which stores signed numbersunless told otherwise. Therefore you can only represent numbers between -32768 and 32767. Your computation of 255 * 255 = 65025 exceeds the range. Thus you see a wrap around. In the C/C++ standard the overflow of signed types is actually undefined behavior. That means that the compiler is allowed to do anything from showing the correct answer to halt and catch fire. Only unsigned types with a known size like uint16_t have a well-deined overflow behaviour.

So in your case you You should use uint16_t instead of long as long as you don't require negative numbers.

Alsopromote your multiplication is a compile time constant which might be a again an overflowed signed int. You can support the compiler by telling it you know what you are doing and you want 255right hand side computation to be unsigned type like this way.:

uint16_t z = 0; 
z = 255U * 255U;

This way you tell the compiler your intention and don't land in "Undefined-Behaviour-Land".

No, this is not a bug. You are using long which stores signed numbers. Therefore you can only represent numbers between -32768 and 32767. Your computation of 255 * 255 = 65025 exceeds the range. Thus you see a wrap around. In the C/C++ standard the overflow of signed types is actually undefined behavior. That means that the compiler is allowed to do anything from showing the correct answer to halt and catch fire. Only unsigned types with a known size like uint16_t have a well-deined overflow behaviour.

So in your case you should use uint16_t instead of long as long as you don't require negative numbers.

Also your multiplication is a compile time constant which might be a again an overflowed signed int. You can support the compiler by telling it you know what you are doing and you want 255 to be unsigned this way.

uint16_t z = 0; 
z = 255U * 255U;

This way you tell the compiler your intention and don't land in "Undefined-Behaviour-Land".

No, this is not a bug. You are using a compile time constant expression which is signed int unless told otherwise. Therefore you can only represent numbers between -32768 and 32767. Your computation of 255 * 255 = 65025 exceeds the range. Thus you see a wrap around. In the C/C++ standard the overflow of signed types is actually undefined behavior. That means that the compiler is allowed to do anything from showing the correct answer to halt and catch fire. Only unsigned types with a known size like uint16_t have a well-deined overflow behaviour. You should promote your right hand side computation to unsigned type like this:

z = 255U * 255U;

This way you tell the compiler your intention and don't land in "Undefined-Behaviour-Land".

Source Link
Kwasmich
  • 1.5k
  • 12
  • 18

No, this is not a bug. You are using long which stores signed numbers. Therefore you can only represent numbers between -32768 and 32767. Your computation of 255 * 255 = 65025 exceeds the range. Thus you see a wrap around. In the C/C++ standard the overflow of signed types is actually undefined behavior. That means that the compiler is allowed to do anything from showing the correct answer to halt and catch fire. Only unsigned types with a known size like uint16_t have a well-deined overflow behaviour.

So in your case you should use uint16_t instead of long as long as you don't require negative numbers.

Also your multiplication is a compile time constant which might be a again an overflowed signed int. You can support the compiler by telling it you know what you are doing and you want 255 to be unsigned this way.

uint16_t z = 0; 
z = 255U * 255U;

This way you tell the compiler your intention and don't land in "Undefined-Behaviour-Land".