Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array of bits

Reply
Thread Tools

array of bits

 
 
Darius Fatakia
Guest
Posts: n/a
 
      01-16-2004
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!


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-16-2004
"Darius Fatakia" <(E-Mail Removed)> writes:

> 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.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-16-2004
On Thu, 15 Jan 2004 17:38:19 -0800, "Darius Fatakia"
<(E-Mail Removed)> wrote in comp.lang.c:

> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-16-2004
Darius Fatakia wrote:
>
> 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.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      01-16-2004
Jack Klein wrote:
> On Thu, 15 Jan 2004 17:38:19 -0800, "Darius Fatakia"
> <(E-Mail Removed)> wrote in comp.lang.c:



>>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?


> 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

 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      01-16-2004
"Jack Klein" <(E-Mail Removed)> wrote:
> 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))

--
Simon.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shifting bits, shift 32 bits on 32 bit int GGG C++ 10 07-06-2006 06:09 AM
what about unsigned and signed 8 bits number, 16 bits, etc?? sarmin kho Python 2 06-15-2004 06:40 PM
8 bits/ch vs 16 bits/ch in PS Terry Digital Photography 5 01-21-2004 06:59 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM



Advertisments