goutham
goutham

Reputation: 848

variable size datatype(int) in c++

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

Upvotes: 2

Views: 449

Answers (2)

Mihir Mehta
Mihir Mehta

Reputation: 13833

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)
};

Upvotes: 3

Inverse
Inverse

Reputation: 4476

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

Upvotes: 1

Related Questions