Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most effective way to store more booleans

I need to store exactly four booleans in my struct in c. Yes I could use four integers or put them into an array but I would like to do it a bit nicer. I was thinking about an int like "0000" where each number would represent the boolean value, but then when editing I cant edit only the one digit, right? That doesnt look perfect either...

Thanks for any ideas

like image 780
tsusanka Avatar asked Dec 01 '22 04:12

tsusanka


1 Answers

You could use a bitfield struct:

struct foo {
  unsigned boolean1 : 1;
  unsigned boolean2 : 1;
  unsigned boolean3 : 1;
  unsigned boolean4 : 1;
};

You can then easily edit each boolean value separately, for example:

struct foo example;
example.boolean1 = 1;
example.boolean2 = 0;
like image 68
houbysoft Avatar answered Dec 04 '22 10:12

houbysoft