bit field with Arrays

Discussion in 'C++' started by Christopher Benson-Manica, Jul 21, 2004.

  1. hack_tick <> spoke thus:

    > struct Tmp
    > {
    > int iVar[20] : 1; // each element of Aray having size as 1-BIT
    > }


    > any suggestions ???


    Yes - vector<bool>.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jul 21, 2004
    #1
    1. Advertising

  2. "hack_tick" <> schreef in bericht
    news:...
    > hi there
    > I was looking for some way to use bit field with Arrays
    > something similar to
    >
    > struct Tmp
    > {
    > int iVar[20] : 1; // each element of Aray having size as 1-BIT
    > }
    >
    > it is possible ??
    > any suggestions ???
    >
    > -regards


    The standard template library uses template specialisation for vector<bool>,
    I think. You can use this explicitly as the std::bitset class.
    Wouter Lievens, Jul 21, 2004
    #2
    1. Advertising

  3. Christopher Benson-Manica

    hack_tick Guest

    hi there
    I was looking for some way to use bit field with Arrays
    something similar to

    struct Tmp
    {
    int iVar[20] : 1; // each element of Aray having size as 1-BIT
    }

    it is possible ??
    any suggestions ???

    -regards
    hack_tick, Jul 21, 2004
    #3
  4. "hack_tick" <> wrote in message
    news:...
    > hi there
    > I was looking for some way to use bit field with Arrays
    > something similar to
    >
    > struct Tmp
    > {
    > int iVar[20] : 1; // each element of Aray having size as 1-BIT
    > }
    >
    > it is possible ??


    No.

    > any suggestions ???
    >


    Use std::bitset instead

    #include <bitset>

    struct Tmp
    {
    std::bitset<20> iVar;
    };

    john
    John Harrison, Jul 21, 2004
    #4
  5. Christopher Benson-Manica

    tom_usenet Guest

    On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <>
    wrote:

    >hi there
    >I was looking for some way to use bit field with Arrays
    >something similar to
    >
    >struct Tmp
    >{
    > int iVar[20] : 1; // each element of Aray having size as 1-BIT
    >}
    >
    >it is possible ??


    Not like that, no.

    >any suggestions ???


    struct Tmp
    {
    std::bitset<20> iVar;
    };

    If you need Tmp to be a POD type, you could code your own POD version
    of bitset. OTOH, bitset is close enough to a POD type to work with
    memcpy with normal compilers.

    Tom
    tom_usenet, Jul 21, 2004
    #5
  6. "hack_tick" <> schreef in bericht
    news:...
    > hi there
    > "Christopher Benson-Manica" <> wrote in message
    > news:cdlr74$2n5$...
    > > hack_tick <> spoke thus:

    > [...]
    > > Yes - vector<bool>.

    >
    > thanks for the suggestion, but sorry i didnt mentioned in my earlier post
    > that
    > I m working on Symbian Series 60 platform, and the struct is a part of
    > protocol which i m using over Bluetooth for communication which is already
    > tooooo heavy so i was looking for some way to reduce the size by using
    > bitfield, also Symbian Series 60 have few bugs and issued with STL so i
    > usually tend to avoid STL with my applications.
    >
    > regards


    Then you'll have to write it yourself - it's not hard.
    Wouter Lievens, Jul 21, 2004
    #6
  7. Christopher Benson-Manica

    hack_tick Guest

    hi there
    "Christopher Benson-Manica" <> wrote in message
    news:cdlr74$2n5$...
    > hack_tick <> spoke thus:

    [...]
    > Yes - vector<bool>.


    thanks for the suggestion, but sorry i didnt mentioned in my earlier post
    that
    I m working on Symbian Series 60 platform, and the struct is a part of
    protocol which i m using over Bluetooth for communication which is already
    tooooo heavy so i was looking for some way to reduce the size by using
    bitfield, also Symbian Series 60 have few bugs and issued with STL so i
    usually tend to avoid STL with my applications.

    regards
    hack_tick, Jul 21, 2004
    #7
  8. "tom_usenet" <> schreef in bericht
    news:...
    > On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <>
    > wrote:
    >
    > >hi there
    > >I was looking for some way to use bit field with Arrays
    > >something similar to
    > >
    > >struct Tmp
    > >{
    > > int iVar[20] : 1; // each element of Aray having size as 1-BIT
    > >}
    > >
    > >it is possible ??

    >
    > Not like that, no.
    >
    > >any suggestions ???

    >
    > struct Tmp
    > {
    > std::bitset<20> iVar;
    > };
    >
    > If you need Tmp to be a POD type, you could code your own POD version
    > of bitset. OTOH, bitset is close enough to a POD type to work with
    > memcpy with normal compilers.
    >
    > Tom


    What is POD?
    Like RAII?
    Wouter Lievens, Jul 21, 2004
    #8
  9. Christopher Benson-Manica

    Sumit Rajan Guest

    Sumit Rajan, Jul 21, 2004
    #9
  10. Christopher Benson-Manica

    tom_usenet Guest

    On Wed, 21 Jul 2004 15:49:55 +0200, "Wouter Lievens"
    <> wrote:

    >"tom_usenet" <> schreef in bericht
    >news:...
    >> On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <>
    >> wrote:
    >>
    >> >hi there
    >> >I was looking for some way to use bit field with Arrays
    >> >something similar to
    >> >
    >> >struct Tmp
    >> >{
    >> > int iVar[20] : 1; // each element of Aray having size as 1-BIT
    >> >}
    >> >
    >> >it is possible ??

    >>
    >> Not like that, no.
    >>
    >> >any suggestions ???

    >>
    >> struct Tmp
    >> {
    >> std::bitset<20> iVar;
    >> };
    >>
    >> If you need Tmp to be a POD type, you could code your own POD version
    >> of bitset. OTOH, bitset is close enough to a POD type to work with
    >> memcpy with normal compilers.
    >>
    >> Tom

    >
    >What is POD?
    >Like RAII?


    In addition to what Sumit posted, the key thing about PODs is that you
    can memcpy them around, which makes them easy to serialize, etc.

    Tom
    tom_usenet, Jul 21, 2004
    #10
  11. "tom_usenet" <> schreef in bericht
    news:...
    > On Wed, 21 Jul 2004 15:49:55 +0200, "Wouter Lievens"
    > <> wrote:
    >
    > >"tom_usenet" <> schreef in bericht
    > >news:...
    > >> On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <>
    > >> wrote:
    > >>
    > >> >hi there
    > >> >I was looking for some way to use bit field with Arrays
    > >> >something similar to
    > >> >
    > >> >struct Tmp
    > >> >{
    > >> > int iVar[20] : 1; // each element of Aray having size as 1-BIT
    > >> >}
    > >> >
    > >> >it is possible ??
    > >>
    > >> Not like that, no.
    > >>
    > >> >any suggestions ???
    > >>
    > >> struct Tmp
    > >> {
    > >> std::bitset<20> iVar;
    > >> };
    > >>
    > >> If you need Tmp to be a POD type, you could code your own POD version
    > >> of bitset. OTOH, bitset is close enough to a POD type to work with
    > >> memcpy with normal compilers.
    > >>
    > >> Tom

    > >
    > >What is POD?
    > >Like RAII?

    >
    > In addition to what Sumit posted, the key thing about PODs is that you
    > can memcpy them around, which makes them easy to serialize, etc.
    >
    > Tom


    Ah I see. Lexical objects is what they are called in data modelling,
    correct?
    So an integer would be one, but a Socket (as in instance of a Socket class)
    would probably not be.
    Wouter Lievens, Jul 22, 2004
    #11
    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. Pratap Das
    Replies:
    1
    Views:
    441
    Richard Heathfield
    Oct 14, 2003
  2. Replies:
    3
    Views:
    1,743
    Timothy Bendfelt
    Jan 19, 2007
  3. JustSomeGuy

    8 bit bit field?

    JustSomeGuy, Feb 27, 2006, in forum: C++
    Replies:
    2
    Views:
    370
    Victor Bazarov
    Feb 27, 2006
  4. Replies:
    9
    Views:
    968
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    172
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page