Connecting Tech Pros Worldwide Help | Site Map

bit field with Arrays

hack_tick
Guest
 
Posts: n/a
#1: Jul 22 '05
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


Christopher Benson-Manica
Guest
 
Posts: n/a
#2: Jul 22 '05

re: bit field with Arrays


hack_tick <hack_tick@yahoo.com> spoke thus:
[color=blue]
> struct Tmp
> {
> int iVar[20] : 1; // each element of Aray having size as 1-BIT
> }[/color]
[color=blue]
> any suggestions ???[/color]

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.
Wouter Lievens
Guest
 
Posts: n/a
#3: Jul 22 '05

re: bit field with Arrays


"hack_tick" <hack_tick@yahoo.com> schreef in bericht
news:2m79dfFjropuU1@uni-berlin.de...[color=blue]
> 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[/color]

The standard template library uses template specialisation for vector<bool>,
I think. You can use this explicitly as the std::bitset class.


John Harrison
Guest
 
Posts: n/a
#4: Jul 22 '05

re: bit field with Arrays



"hack_tick" <hack_tick@yahoo.com> wrote in message
news:2m79dfFjropuU1@uni-berlin.de...[color=blue]
> 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 ??[/color]

No.
[color=blue]
> any suggestions ???
>[/color]

Use std::bitset instead

#include <bitset>

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

john


hack_tick
Guest
 
Posts: n/a
#5: Jul 22 '05

re: bit field with Arrays


hi there
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:cdlr74$2n5$1@chessie.cirr.com...[color=blue]
> hack_tick <hack_tick@yahoo.com> spoke thus:[/color]
[...][color=blue]
> Yes - vector<bool>.[/color]

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


tom_usenet
Guest
 
Posts: n/a
#6: Jul 22 '05

re: bit field with Arrays


On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <hack_tick@yahoo.com>
wrote:
[color=blue]
>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 ??[/color]

Not like that, no.
[color=blue]
>any suggestions ???[/color]

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
Wouter Lievens
Guest
 
Posts: n/a
#7: Jul 22 '05

re: bit field with Arrays


"hack_tick" <hack_tick@yahoo.com> schreef in bericht
news:2m7a23Fjri70U1@uni-berlin.de...[color=blue]
> hi there
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
> news:cdlr74$2n5$1@chessie.cirr.com...[color=green]
> > hack_tick <hack_tick@yahoo.com> spoke thus:[/color]
> [...][color=green]
> > Yes - vector<bool>.[/color]
>
> 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[/color]

Then you'll have to write it yourself - it's not hard.


Wouter Lievens
Guest
 
Posts: n/a
#8: Jul 22 '05

re: bit field with Arrays


"tom_usenet" <tom_usenet@hotmail.com> schreef in bericht
news:2ussf05mjabqna24de8q1p12oq10hs63q3@4ax.com...[color=blue]
> On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <hack_tick@yahoo.com>
> wrote:
>[color=green]
> >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 ??[/color]
>
> Not like that, no.
>[color=green]
> >any suggestions ???[/color]
>
> 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[/color]

What is POD?
Like RAII?


Sumit Rajan
Guest
 
Posts: n/a
#9: Jul 22 '05

re: bit field with Arrays



"Wouter Lievens" <lievenswouter@snotmail.com> wrote in message
news:40fe747e$0$3971$ba620e4c@news.skynet.be...
[color=blue]
> What is POD?
> Like RAII?[/color]

Plain Old Data.

Try:
http://www.parashift.com/c++-faq-lit....html#faq-26.7

Regards,
Sumit.
--
Sumit Rajan <sumitrajan@myrealbox.com>


tom_usenet
Guest
 
Posts: n/a
#10: Jul 22 '05

re: bit field with Arrays


On Wed, 21 Jul 2004 15:49:55 +0200, "Wouter Lievens"
<lievenswouter@snotmail.com> wrote:
[color=blue]
>"tom_usenet" <tom_usenet@hotmail.com> schreef in bericht
>news:2ussf05mjabqna24de8q1p12oq10hs63q3@4ax.com.. .[color=green]
>> On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <hack_tick@yahoo.com>
>> wrote:
>>[color=darkred]
>> >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 ??[/color]
>>
>> Not like that, no.
>>[color=darkred]
>> >any suggestions ???[/color]
>>
>> 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[/color]
>
>What is POD?
>Like RAII?[/color]

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
Wouter Lievens
Guest
 
Posts: n/a
#11: Jul 22 '05

re: bit field with Arrays


"tom_usenet" <tom_usenet@hotmail.com> schreef in bericht
news:favsf0tntc4isahsqolespr8lvgvf9dcn2@4ax.com...[color=blue]
> On Wed, 21 Jul 2004 15:49:55 +0200, "Wouter Lievens"
> <lievenswouter@snotmail.com> wrote:
>[color=green]
> >"tom_usenet" <tom_usenet@hotmail.com> schreef in bericht
> >news:2ussf05mjabqna24de8q1p12oq10hs63q3@4ax.com.. .[color=darkred]
> >> On Wed, 21 Jul 2004 19:08:23 +0530, "hack_tick" <hack_tick@yahoo.com>
> >> 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[/color]
> >
> >What is POD?
> >Like RAII?[/color]
>
> 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[/color]

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.


Closed Thread


Similar C / C++ bytes