Public Group

# Binary Notation

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

## Recommended Posts

How do I use binary notation in my C++ source code? I've tried 0b10010101, b10010101, 01010101b but they all give compile errors. Google didn’t yield any thing helpful either.

##### Share on other sites
Perhaps writing a binary helper function would help?

x = binaryfunction("10110101");

?

From,
Nice coder

##### Share on other sites
As far as I am aware no major C/C++ compiler directly supports binary numeric literals. Generally, the standard literal bases supported are decimal, octal, and hexadecimal.

What do you need this for? Perhaps there is an acceptable alternative solution for what you're intending to accomplish.

##### Share on other sites
You can use hex no problem, at least in C/C++.

If you don't know, to convert from binary to hex, it's really easy. Just convert each group of 4 binary digits into 1 hex digit.

0000 = 0
0001 = 1
0010 = 2
0011 = 3
...etc...
1010 = a
1011 = b
1100 = c
1101 = d
1110 = e
1111 = f

So, 110101001 in hex is just 1a9. This is also an example of what to do when the number of binary digits you have isn't a multiple of 4, just padd the front with 0's. It basically makes no difference to the end number.

In C/C++, in the actual code you have to write the hex number with a preceeding '0x', so, the hex number in C/C++ code would be 0x1a9.

##### Share on other sites
Quote:
 Original post by Nice CoderPerhaps writing a binary helper function would help?x = binaryfunction("10110101");?From,Nice coder

Interesting, never thought of that. I guess I’ll try that. Although now that I think about it would be unnecessary CPU cycles used there.

As to what I need it for: Boolean flags packed in to a single variable. Its not really necessary just convenient. It’s a head ache to have and go convert by hand or by windows calculator each time I need a binary number. It really should be a standard IDE feature.

##### Share on other sites
Use bitwise operators like OR (|) or AND (&). For example:

#define FLAG1 0x01 // 001#define FLAG2 0x02 // 010#define FLAG3 0x04 // 100int flags = 0; // 000flags = flags | FLAG2; // add FLAG2: 010flags = flags | FLAG3; // add FLAG3: 110// note these operations are independent - none of them affect the others.// to test:if(flags & FLAG1) // 110 & 001 = 000, which is false, therefore the if fails{    // FLAG1 set}if(flags & FLAG3) // 110 & 100 = 100 = 4, which is true, therefore the if succeeds{    // FLAG3 set}

##### Share on other sites
Yeah that’s pretty much what I am doing now.

Its would just be easier to write flags = 01110101; as opposed to flags = FLAG1 | FLAG3 | FLAG5 | FLAG6 | FLAG7;

##### Share on other sites
Here's a piece of compile-time magic I came up with a while ago. If you want longer than 9 digit binary literals, you need to use non-standard integer types, though (long long in gcc's case).

#include <iostream>typedef unsigned long long literal_t;template <literal_t i, unsigned int base>struct bc{  enum { r = (i % 10) + (bc<i / 10, base>::r * base) };};template <unsigned int base>struct bc<0, base>{  enum { r = 0 };};template <literal_t i>struct bin{  enum { r = bc<i, 2>::r };};int main(){  std::cout <<  bin<10010010>::r << '\n'; // Outputs 146}

##### Share on other sites
Quote:
 Original post by GrainIt would just be easier to write flags = 01110101; as opposed to flags = FLAG1 | FLAG3 | FLAG5 | FLAG6 | FLAG7;

Easier to write, perhaps. You will, however, be glad that you used FLAG1 | FLAG2 | ... when you try to read the code again a year from now.

##### Share on other sites
Yea no joke! How would you know what flag did what? and with named flags you can combine them with OR in any order instead of wondering which digit represented what flag.

in other words...

FLAG1 | FLAG2 = FLAG2 | FLAG1

but 01 != 10 See the convenience?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 31
• 16
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
634117
• Total Posts
3015601
×