bit field with Arrays

  • Thread starter Christopher Benson-Manica
  • Start date
W

Wouter Lievens

hack_tick said:
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.
 
H

hack_tick

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
 
J

John Harrison

hack_tick said:
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
 
T

tom_usenet

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
 
W

Wouter Lievens

hack_tick said:
hi there
Christopher Benson-Manica said:
hack_tick <[email protected]> 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.
 
H

hack_tick

hi there
Christopher Benson-Manica said:
hack_tick <[email protected]> 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
 
W

Wouter Lievens

tom_usenet said:
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?
 
T

tom_usenet

tom_usenet said:
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
 
W

Wouter Lievens

tom_usenet said:
tom_usenet said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top