Is there a way to represent binary numbers in your source code? I was certain I saw it somewhere looking like the following:
I may be getting confused with another language however...Code:int b = 01101001b;
Printable View
Is there a way to represent binary numbers in your source code? I was certain I saw it somewhere looking like the following:
I may be getting confused with another language however...Code:int b = 01101001b;
You can use hex:
int b = 0x69;
// 69 hex = 01101001 binary
Nice. So I have to sit there and convert it with my calculator. Why would the ability to state binary numbers have not been included in the language?
Quote:
Originally Posted by ahluka
Becuase hex is easier to use, and there's little point in having binary shown explicitly
ghtu bnj
>So I have to sit there and convert it with my calculator.
Or learn hexadecimal so that you can easily make the conversion in your head.
>Why would the ability to state binary numbers have not been included in the language?
Because it's not necessary when hexadecimal literals convey the same information just as easily. Because it's too error prone for all but the smallest types. And most importantly, because the designers of the language were conspiring to irritate you personally. You'll notice that the earliest specifications for C++ included this gem (sadly, it was removed shortly after to avoid a paper trail):
The binary literal, believed by many to be the most useful feature in programming history, was left out of C++ to annoy ahluka. We feel that the amusing results will outweigh any lack of clarity brought on by requiring hexadecimal literals when a binary literal is desired.
Bah, it's not that hard. If you know binary up to 16, then you can do it easily.Quote:
Originally Posted by ahluka
Of course, you've got to remember digits A-F, but that's not too bad.Code:Start with: 01101001
Break into nibbles: 0110 1001
Convert each nibble: 6 9
Put back together: 0x69
Code:Start with: 11101101
Break into nibbles: 1110 1101
Convert each nibble: E D
Put back together: 0xED
Who needs calculators when you have STL
or even a class!Code:#include <iostream>
#include <bitset>
#include <string>
int BinToDec(const std::string& str)
{
return std::bitset<8>(str).to_ulong();
}
int main()
{
int x = BinToDec("10101010");
std::cout << x;
}
Code:#include <iostream>
#include <bitset>
#include <string>
class Binary
{
int m_x;
public:
Binary(const std::string& str)
{
m_x = std::bitset<8>(str).to_ulong();
}
operator int()
{
return m_x;
}
};
int main()
{
Binary b("10101010");
int x = b;
std::cout << x;
}
Code:#include <bitset>
#include <iostream>
#include <string>
using namespace std;
int main()
{
bitset<32> b ( string ( "11111111" ) );
cout<< b <<'\n'
<< static_cast<char> ( b.to_ulong() ) <<'\n'
<< static_cast<int> ( b.to_ulong() ) <<'\n';
cin.get();
return 0;
}
an appended letter to specify radix is an assembly thing, if you want to use binary in C/C++:
(compiles under visual studio) __asm mov b,01101001b
Wow guys. I leave you alone for a couple of hours and you throw all these answers at me. Ta muchly all o' ya! :)
:eek: How dare they. Y'know, I just knew somebody was against me, and know I know for sure.Quote:
The binary literal, believed by many to be the most useful feature in programming history, was left out of C++ to annoy ahluka. We feel that the amusing results will outweigh any lack of clarity brought on by requiring hexadecimal literals when a binary literal is desired
A-ha, so that's where I saw the 'b' suffix.Quote:
an appended letter to specify radix is an assembly thing, if you want to use binary in C/C++:
(compiles under visual studio) __asm mov b,01101001b