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

前端 未结 4 1504
遇见更好的自我
遇见更好的自我 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条回答
  •  -上瘾入骨i
    2021-01-17 10:12

    First of all, just to clarify thing, crafting a negative zero using bitwise operations and then using the resulting value is not portable. That said, nothing specifies in the documentation of fprintf (thus, of std::basic_ostream::operator<<(int)) whether the sign bit in the representation of int corresponds to a padding bit in the representation of unsigned or an actual value bit.

    As a conclusion, this is unspecified behaviour.

    #include 
    
    int main() {
        std::cout << ~0 << std::endl;
        return 0;
    }
    

提交回复
热议问题