-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binary string to cpp_int #297
Comments
This is an interesting post. Boost.Multiprecision is transitioning to C++11 for the next release. Literal binary strings, if memory serves, arrived in C++14. Boost.Multiprecision is, however, not strictly tied to that exact language evolutionary step. Does auto-detect of binary-string-to-multiprecision type make sense? And if so, for which (or all) or our backends? |
There are really two features here:
The former is valid for all integer backends, the latter only for cpp_int, and need some hairy-scary modification to the existing user-defined-literal code ('tis horrible to write). For iostream code, it should really be possible to write it in backend-agnostic format. While we're at it, we should support ' separators in numbers as well. |
Indeed. |
I would suggest it be based on std::bitset as it covers both of those requirements @jzmaddock . #include <iostream>
#include <bitset>
int main(void)
{
constexpr unsigned int myNum = 3;
constexpr std::bitset< sizeof(myNum) * 8 > bits(myNum);
std::cout << bits << std::endl;
return 0;
}
//Compiled with -std=c++11
//Output: 00000000000000000000000000000011 As for arbitrary size literals, we can base it off of boost::dynamic_bitset #include <iostream>
#include <boost/dynamic_bitset.hpp>
int main(void)
{
unsigned int myNum = 3;
boost::dynamic_bitset<> bits(sizeof(myNum) * 8, myNum);
std::cout << bits << std::endl;
return 0;
} |
Would love to have the octal |
Octal has always been supported, but it's 0123, there's no "o" letter in C++ octal strings. |
Thanks for @aaangeletakis solution! One thing to note, the compiler warns me that "comparison of unsigned expression in ‘>= 0’ is always true." Maybe we can change the types of
|
Hello, I noticed boost multiprecision does not have a binary string to decimal converter, so I made one. Hope you enjoy!
The text was updated successfully, but these errors were encountered: