Packing 4 Integers as ONE BYTE?

前端 未结 4 1276
予麋鹿
予麋鹿 2020-12-16 13:35

I have four integers {a, b, c, d} that can have the following range of values:

a - {0 or 1} (1 bi

4条回答
  •  清歌不尽
    2020-12-16 14:29

    def encode(a, b, c, d):
      return a | b << 1 | c << 2 | d << 5
    
    def decode(x):
      return x & 1, (x >> 1) & 1, (x >> 2) & 7, (x >> 5) & 7
    

提交回复
热议问题