Jump to content
  • Advertisement
Sign in to follow this  
Grain

Binary Notation

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Nice Coder
Perhaps 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 this post


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


#define FLAG1 0x01 // 001
#define FLAG2 0x02 // 010
#define FLAG3 0x04 // 100

int flags = 0; // 000

flags = flags | FLAG2; // add FLAG2: 010
flags = 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
}



For more info check this article out.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Grain
It 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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!