How does std::cout print negative zero in a ones-complement system?

前端 未结 4 1538
遇见更好的自我
遇见更好的自我 2021-01-17 09:34

On a ones-complement platform, what would the following code print?

#include 

int main() {
    int i = 1, j = -1;

    std::cout << i+         


        
4条回答
  •  既然无缘
    2021-01-17 10:19

    Indeed adding n to -n should give you a negative zero. But the generation of -0 doesn't happen in practice since 1's complement addition uses a technique called a complementing subtractor (the second argument is complemented and subtracted from the first).

    (The idiomatic way of getting a signed floating point zero doesn't apply here since you can't divide an integer by zero).

提交回复
热议问题