How to create a fixed size (unsigned) integer in python?

后端 未结 4 963
渐次进展
渐次进展 2021-01-13 02:39

I want to create a fixed size integer in python, for example 4 bytes. Coming from a C background, I expected that all the primitive types will occupy a constant space in mem

4条回答
  •  半阙折子戏
    2021-01-13 03:26

    I have no idea if there's a better way to do this, but here's my naive approach:

    def intn(n, num_bits=4):
        return min(2 ** num_bits - 1, n)
    

提交回复
热议问题