Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   bit field with Arrays (http://www.velocityreviews.com/forums/t284507-bit-field-with-arrays.html)

Christopher Benson-Manica 07-21-2004 01:31 PM

Re: bit field with Arrays
 
hack_tick <hack_tick@yahoo.com> 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.

Wouter Lievens 07-21-2004 01:32 PM

Re: bit field with Arrays
 
"hack_tick" <hack_tick@yahoo.com> schreef in bericht
news:2m79dfFjropuU1@uni-berlin.de...
> 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.



hack_tick 07-21-2004 01:38 PM

bit field with Arrays
 
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



John Harrison 07-21-2004 01:42 PM

Re: bit field with Arrays
 

"hack_tick" <hack_tick@yahoo.com> wrote in message
news:2m79dfFjropuU1@uni-berlin.de...
> 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



tom_usenet 07-21-2004 01:48 PM

Re: bit field with Arrays
 
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

Wouter Lievens 07-21-2004 01:49 PM

Re: bit field with Arrays
 
"hack_tick" <hack_tick@yahoo.com> schreef in bericht
news:2m7a23Fjri70U1@uni-berlin.de...
> hi there
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
> news:cdlr74$2n5$1@chessie.cirr.com...
> > hack_tick <hack_tick@yahoo.com> 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.



hack_tick 07-21-2004 01:49 PM

Re: bit field with Arrays
 
hi there
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:cdlr74$2n5$1@chessie.cirr.com...
> hack_tick <hack_tick@yahoo.com> 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



Wouter Lievens 07-21-2004 01:49 PM

Re: bit field with Arrays
 
"tom_usenet" <tom_usenet@hotmail.com> schreef in bericht
news:2ussf05mjabqna24de8q1p12oq10hs63q3@4ax.com...
> 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


What is POD?
Like RAII?



Sumit Rajan 07-21-2004 02:05 PM

Re: bit field with Arrays
 

"Wouter Lievens" <lievenswouter@snotmail.com> wrote in message
news:40fe747e$0$3971$ba620e4c@news.skynet.be...

> What is POD?
> Like RAII?


Plain Old Data.

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

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



tom_usenet 07-21-2004 02:29 PM

Re: bit field with Arrays
 
On Wed, 21 Jul 2004 15:49:55 +0200, "Wouter Lievens"
<lievenswouter@snotmail.com> wrote:

>"tom_usenet" <tom_usenet@hotmail.com> schreef in bericht
>news:2ussf05mjabqna24de8q1p12oq10hs63q3@4ax.com.. .
>> 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

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


All times are GMT. The time now is 08:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.