tags:

views:

94

answers:

2

Hello I want to use a datatype to represent sizes like 3bits or 12bits.. can anyone tell me how can I implement this in c++

and is there any code like this, which can help me define size in bits

int i:3;

thanks in advance ..

+3  A: 

you can use structure like this

struct Date {
   unsigned short nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned short nMonthDay : 5;    // 0..31  (6 bits)
   unsigned short nMonth    : 4;    // 0..12  (5 bits)
   unsigned short nYear     : 7;    // 0..100 (8 bits)
};
mihirpmehta
You only need 5 bits for nMonthDay, 4 bits for nMonth and 7 bits for nYear (if you only want to go from 0 to 100).
Omnifarious
True...you are right...
mihirpmehta
+1  A: 

You can use the vector<bool> specialization class

std::vector<bool> bits(3);

or, boost's dynamic_bitset class

boost::dynamic_bitset<> bits(3);

http://www.boost.org/doc/libs/1_42_0/libs/dynamic_bitset/dynamic_bitset.html

Inverse