Jump to content
  • Advertisement
Sign in to follow this  
Yourself

[C++] Arithmetic operations with boost::dynamic_bitset

This topic is 2993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi Is it possible to do an arithmetic operation on 2 boost::dynamic_bitsets ?? say I want to do something like this :
boost::dynamic_bitset<> a(3,3);
boost::dynamic_bitset<> b(3,3);
boost::dynamic_bitset<> c(3,0);
c = a+b; //I would want c to have a value of 6 (110)

there is no support for the arithmetic operators but say I do want this, can anybody point me into the right direction? cheers

Share this post


Link to post
Share on other sites
Advertisement
I don't have much experience with dynamic_bitset, but from just glancing through the interface you should be able to do:

dynamic_bitset<> c(max(a.size(),b.size())+1, a.to_ulong() b.to_ulong());

Where the +1 is to handle potential overflow. Note that this isn't particular efficient if a and b have a lot of 0 padding on the most significant end, so if that's an issue you'll need a more clever approach (probably doing the bitwise addition yourself).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!