求某个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));


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2021-10-15
  • 2021-12-24
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2021-08-29
  • 2021-08-30
  • 2022-12-23
相关资源
相似解决方案