I am not specific about any algorithm or program. But considering bit manipulation programs and other tasks that involves 2's complement or 1's complement etc., what if the negative numbers are represented (in memory or wherever) in a way opposite to the assumptions of the programmer. Does this scenario even occur? If yes, then how can it be handled.
Asked
Active
Viewed 60 times
0
-
It is very common that something works on a platform differently (your "etc.") than programmer assumed. Defective software will refuse to compile or fail tests. Defects are handled by programming non-defective code. – Öö Tiib Feb 20 '23 at 08:15
-
why specifically negative numbers? There are different ways to store positives too – 463035818_is_not_an_ai Feb 20 '23 at 08:22
-
somewhat related https://stackoverflow.com/questions/70801443/why-is-signed-overflow-due-to-computation-still-undefined-behavior-in-c20 – 463035818_is_not_an_ai Feb 20 '23 at 08:35
1 Answers
3
Does this scenario even occur?
Yes, before C++20 that is allowed and there have been architectures that don't use two's complement in the past. However since C++20 two's complement representation is mandated.
If yes, then how can it be handled.
By not relying on operations with implementation-defined behavior or by asserting that your code may only be used on two's complement architectures.

user17732522
- 53,019
- 2
- 56
- 105