6

I am in need of implementing an efficient bit array in C. From what I have seen C does not support this so you can use an array of integers (according to one site I looked at) and then use a shift to access individual bits. Would simply declaring a bool array be the same thing or is this less memory efficient?

9
  • 2
    A _bool in C is generally an int internally. A _bool[] uses about 32 times (give or take) as much memory as the array of integers with bit-shifted access. Commented Nov 17, 2014 at 22:42
  • 1
    If using C++ is a viable option for you, std::vector<bool> offers the bit-packing space optimization you're describing. Commented Nov 17, 2014 at 22:48
  • 2
    @EOF: _Bool (not _bool) is typically 1 byte, not the same size as int. Commented Nov 17, 2014 at 22:48
  • @KeithThompson: Yeah, you're right. So it's just a factor of CHAR_BIT. I said give or take. You've just taken a lot... Commented Nov 17, 2014 at 22:49
  • Sadly C++ is not an option. Commented Nov 17, 2014 at 22:50

1 Answer 1

6

Yes, a simple _Bool array require more storage than an array of integers combined with some bit-shifting. The _Bool array stores one bit of data in a sizeof(_Bool) space (normally a single byte). The integer array can store many more bits per byte (minimum 8).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.