How to implement a bitset in C?

2019-01-08 19:27发布

I have been using the Bitset class in Java and I would like to do something similar in C. I suppose I would have to do it manually as most stuff in C. What would be an efficient way to implement?

byte bitset[]

maybe

bool bitset[]

?

标签: c bitset
7条回答
神经病院院长
2楼-- · 2019-01-08 20:13

Well, byte bitset[] seems a little misleading, no?

Use bit fields in a struct and then you can maintain a collection of these types (or use them otherwise as you see fit)

struct packed_struct {
  unsigned int b1:1;
  unsigned int b2:1;
  unsigned int b3:1;
  unsigned int b4:1;
  /* etc. */
} packed;
查看更多
登录 后发表回答