(Michael Chinen)

debugged

30 years coding and still running into unsigned int bugs

mchinen

I came across another unsigned int issue a year or so ago and for some reason I didn’t post it.  Maybe I was embarrassed?  Anyway that Mike is dead now, mostly replaced by new cells, so I’m posting it. It’s not a totally surprising either but I’ll post it anyway.

If you have an unsigned int and you negate it with the unary ‘-‘ operator, you will just get a really large unsigned integer value, and not a negative value.

From my last post about unsigned int, you can’t do

smallInt - unsignedInt

and expect a negative value because this converts the int to unsigned int.
That was for operators that worked on two values.

With the unary ‘-‘ operator there is no conversion to signed int.
According to this stack overflow post the value you get is

UINT_MAX - origUIntValue + 1

That seems to make sense.
Here’s my lldb (xcode’s gdb replacement for llvm) output:

(lldb) print -m_labelTex.pixelsWide
(NSUInteger) $3 = 4294967232
(lldb) print m_labelTex.pixelsWide
(NSUInteger) $4 = 64
(lldb) print -((int)m_labelTex.pixelsWide)
(int) $6 = -64
(lldb) print 0 - m_labelTex.pixelsWide
(unsigned int) $7 = 4294967232
(lldb) print ((int) 4) - m_labelTex.pixelsWide
(unsigned int) $8 = 4294967236
Back to top