INT_MIN绝对值
求某个int32的绝对值:
unsigned int abs_n = n < 0 ? UINT_MAX - ((unsigned int)(n)) + 1U
: (unsigned int)(n);
Update: As @aka.nice suggests, we can actually replace UINT_MAX + 1U
by 0U
:
unsigned int abs_n = n < 0 ? -((unsigned int)(n))
: +((unsigned int)(n));