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));


posted on 2021-12-20 21:17  wsw_seu  阅读(34)  评论(0编辑  收藏  举报

导航