开发者

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开发者_StackOverflow code like this, which can help me define size in bits

int i:3;

thanks in advance ..


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜