array of bits

D

Darius Fatakia

I feel like I might be overlooking something obvious, but...

I want to store an array of bits (0's and 1's) in the cheapest possible way.
The array might be 1,000 to 10,000 elements long. What variable type/
structure should I use?

Thanks!
 
B

Ben Pfaff

Darius Fatakia said:
I feel like I might be overlooking something obvious, but...

I want to store an array of bits (0's and 1's) in the cheapest possible way.
The array might be 1,000 to 10,000 elements long. What variable type/
structure should I use?

Use an array of integer type (`unsigned char' or `unsigned int',
for instance), then use bitwise operators to access individual
bits.
 
J

Jack Klein

I feel like I might be overlooking something obvious, but...

I want to store an array of bits (0's and 1's) in the cheapest possible way.
The array might be 1,000 to 10,000 elements long. What variable type/
structure should I use?

Thanks!

You must have somehow missed this question (and its answer) when you
read the FAQ for this group, as you are supposed to and surely did
before you posted here:

20.8 How can I implement sets or arrays of bits?

There's a link to the FAQ for comp.lang.c, which includes the answer
to this question as well as many, many others, in my signature block.
 
C

CBFalconer

Darius said:
I feel like I might be overlooking something obvious, but...

It's called the FAQ.
I want to store an array of bits (0's and 1's) in the cheapest
possible way. The array might be 1,000 to 10,000 elements long.
What variable type/ structure should I use?

Something that can hold 10,000 bits. I suggest you start with:

#define BITCOUNTMAX 10000

and go on from there. Define cheap.
 
G

glen herrmannsfeldt

Jack said:
On Thu, 15 Jan 2004 17:38:19 -0800, "Darius Fatakia"
You must have somehow missed this question (and its answer) when you
read the FAQ for this group, as you are supposed to and surely did
before you posted here:

20.8 How can I implement sets or arrays of bits?

There's a link to the FAQ for comp.lang.c, which includes the answer
to this question as well as many, many others, in my signature block.

He didn't just ask how he can implement array of bits, but
how to do it in the cheapest way.

Unfortunately he didn't say what cheap was.

On some machines the fastest (cheapest execution time) is to store
each bit in a single char or int array element.

Cheapest in memory used is to use the logical operators and
store the appropriate number of bits in an unsigned char or
unsigned int array element.

You might need to try both to see which is best on your machine.

-- glen
 
S

Simon Biber

Jack Klein said:
You must have somehow missed this question (and its answer)
when you read the FAQ for this group, as you are supposed
to and surely did before you posted here:

20.8 How can I implement sets or arrays of bits?

The macros given in the FAQ:
#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

Seem to be missing a way to clear a bit?
#define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b))

May as well add a toggler too:
#define BITFLIP(a, b) ((a)[BITSLOT(b)] ^= BITMASK(b))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top