I need to work with a binary number.
I tried writing:
const char x = 00010000;
But it didn’t work.
I know that I can use a hexadecimal number that has the same value as 00010000, but I want to know if there is a type in C++ for binary numbers, and if there isn’t, is there another solution for my problem?
You can use
BOOST_BINARYwhile waiting for C++0x. 🙂BOOST_BINARYarguably has an advantage over template implementation insofar as it can be used in C programs as well (it is 100% preprocessor-driven.)To do the converse (i.e. print out a number in binary form), you can use the non-portable
itoafunction, or implement your own.Unfortunately you cannot do base 2 formatting with STL streams (since
setbasewill only honour bases 8, 10 and 16), but you can use either astd::stringversion ofitoa, or (the more concise, yet marginally less efficient)std::bitset.produces:
Also read Herb Sutter’s The String Formatters of Manor Farm for an interesting discussion.