The “unsigned” Conundrum--Tony “Bulldozer00” DaSilva

Are you clear with unsigned?

The “unsigned” Conundrum

by Tony “Bulldozer00” DaSilva

From the article:

A few weeks ago, CppCon16 conference organizer Jon Kalb gave a great little lightning talk titled “unsigned: A Guideline For Better Code“. Right up front, he asked the audience what they thought this code would print out to the standard console:

Even though -1 is obviously less 1, the program prints out “a is not less than b“. WTF?

Add a Comment

Comments are closed.

Comments (1)

0 0

szOldAndInTheWay said on Oct 19, 2016 10:32 AM:

At the point of comparison, the compiler treats both variables as unsigned.
The hex value of a is 0xffffffff.
The hex value of b is 0x00000001.

So the comparison
(0xfffffff < 0x0000001) is false.

If you want to compare the signed values you need to cast b to signed.