array of bits

Discussion in 'C Programming' started by Darius Fatakia, Jan 16, 2004.

  1. 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!
    Darius Fatakia, Jan 16, 2004
    #1
    1. Advertising

  2. Darius Fatakia

    Ben Pfaff Guest

    "Darius Fatakia" <> 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
    Ben Pfaff, Jan 16, 2004
    #2
    1. Advertising

  3. Darius Fatakia

    Jack Klein Guest

    On Thu, 15 Jan 2004 17:38:19 -0800, "Darius Fatakia"
    <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 16, 2004
    #3
  4. Darius Fatakia

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jan 16, 2004
    #4
  5. Jack Klein wrote:
    > On Thu, 15 Jan 2004 17:38:19 -0800, "Darius Fatakia"
    > <> 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
    glen herrmannsfeldt, Jan 16, 2004
    #5
  6. Darius Fatakia

    Simon Biber Guest

    "Jack Klein" <> 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.
    Simon Biber, Jan 16, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    12,550
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    825
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    443
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. sergey

    "casting" bits to bits?

    sergey, Nov 8, 2006, in forum: VHDL
    Replies:
    1
    Views:
    704
    sergey
    Nov 8, 2006
  5. Tomás

    Value Bits Vs Object Bits

    Tomás, Jun 2, 2006, in forum: C Programming
    Replies:
    13
    Views:
    546
    Hallvard B Furuseth
    Jul 1, 2006
Loading...

Share This Page