# Thread: Oh wonderful binary, where art thou?

1. ## Oh wonderful binary, where art thou?

Is there a way to represent binary numbers in your source code? I was certain I saw it somewhere looking like the following:

Code:
` int b = 01101001b;`
I may be getting confused with another language however...

2. You can use hex:

int b = 0x69;

// 69 hex = 01101001 binary

3. 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?

4. Originally Posted by ahluka
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?

Becuase hex is easier to use, and there's little point in having binary shown explicitly

5. ghtu bnj

6. >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?
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.

7. Originally Posted by ahluka
Nice. So I have to sit there and convert it with my calculator.
Bah, it's not that hard. If you know binary up to 16, then you can do it easily.
Code:
```Start with:         01101001
Break into nibbles: 0110 1001
Convert each nibble:  6    9
Put back together:     0x69```
Of course, you've got to remember digits A-F, but that's not too bad.
Code:
```Start with:         11101101
Break into nibbles: 1110 1101
Convert each nibble:  E    D
Put back together:     0xED```

8. Who needs calculators when you have STL

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;
}```
or even a class!

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

9. 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;
}```

10. 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

11. Wow guys. I leave you alone for a couple of hours and you throw all these answers at me. Ta muchly all o' ya!

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
How dare they. Y'know, I just knew somebody was against me, and know I know for sure.

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
A-ha, so that's where I saw the 'b' suffix.