short short int in c?

Griffin picture Griffin · Jul 14, 2011 · Viewed 8.7k times · Source

I'm trying to squeeze as much out of my memory as possible. I have a matrix of 4.9999995e13 ints but they only need to be true or false - basically I only need one bit of storage for each of these ints.

I understand that there are no single bit types in C (maybe someone can explain why, to me), and I also know that if a short short int existed it would be 1 byte, same as char. However all of the logical operations in C return ints (as well as a few other functions).

So my questions are:

  • Is there some way of making a short short int exist?
  • If I was to use char instead, would I have performance decrease because of all the casting to int that would have to be done?
  • Is there another way that I'm missing?

Just in-case it's relevant, I am compiling with GCC for C99.

EDIT I've just seen on this wikipedia page that there is a _Bool type, is this actually standard?

Answer

JSBձոգչ picture JSBձոգչ · Jul 14, 2011

The __Bool type is standard in the most recent version of C, but that's still not what you want, because a __Bool still takes up at least one byte (as does a char, by definition).

No, if you want that many boolean bits you need to pack them into a bitfield or bit array. There is no standard datatype for bitfields in C, so you're also going to have to write your own macros or functions for getting the bit at a particular offset. I also hope that you're going to run this on a 64-bit machine with plenty of RAM, otherwise you're going to run out of memory and fast.