Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 1 not greater than -0x80000000 [duplicate]

Tags:

c++

c

Why is 1 not greater than -0x80000000. I know it has something to do with overflow. But can someone explain why? is 0x80000000 not a constant I think it is?

assert(1 > -0x80000000);

The assert triggers in C++. Why is that?


I am grateful for some of the answer provided. But does C++ standard define that the constant needs to be stored in a 32 bit integer? Why doesn't compiler recognized that 80000000 isn't going to be fit for a 32 bit integer and use 64 bit for it? I mean, the largest 32 bit int can be 0x7FFFFFFF. 0x80000000 is obviously larger than that. Why does compiler still use 32 bit for that?

like image 480
Negative Zero Avatar asked Nov 21 '14 01:11

Negative Zero


People also ask

Is duplicate data allowed in set 1?

Set is not allowed to store duplicated values by definition. If you need duplicated values, use a List. As specified on the documentation of the interface, when you try to add a duplicated value, the method add returns false, not an Exception.

Why is duplicate values not working?

Trailing or leading spaces Probably the most common cause of Excel not recognizing duplicates. Check if the one cell has trailing, leading or extra spaces in the cell. Excel sees the space as an individual character but humans tend to ignore it.

What is a duplicate value?

A duplicate value is one in which all values in at least one row are identical to all of the values in another row. A comparison of duplicate values depends on the what appears in the cell—not the underlying value stored in the cell.


1 Answers

According to the C and C++ standards, -0x80000000 is not an integer constant. It's an expression, like 3 + 5. In this case, it's the constant 0x80000000, operated upon by the negation operator. For compilers which have 32-bit ints, 0x80000000 is not representable as an int, but is representable as an unsigned int. But negating an unsigned integer is (weirdly) done in an unsigned context. So the negation here effectively has no effect.

like image 140
Sneftel Avatar answered Oct 18 '22 01:10

Sneftel