C question: off_t (and other signed integer types) minimum and maximum values

后端 未结 9 1026
耶瑟儿~
耶瑟儿~ 2020-12-06 05:58

I occasionally will come across an integer type (e.g. POSIX signed integer type off_t) where it would be helpful to have a macro for its minimum and maximum val

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 06:26

    For sign-magnitude representations, it's fairly easy (for types at least as wide as int, anyway):

    #define SM_TYPE_MAX(type) (~(type)-1 + 1)
    #define SM_TYPE_MIN(type) (-TYPE_MAX(type))
    

    Unfortunately, sign-magnitude representations are rather thin on the ground ;)

提交回复
热议问题