Sign in to follow this  
Grain

Binary Notation

Recommended Posts

Grain    500
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
ApochPiQ    23061
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
Endar    668
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
Grain    500
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
baldurk    1028
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
Grain    500
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
Sharlin    864
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
Icefox    238
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
chad_420    290
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
Ikana    160
If you are just doing flags, can't you do something like


typedef struct flags{
unsigned short int flag1:1,flag2:1,flag3:1;
}flags;

int foo(){
flags flagset;
flagset.flag1 = 1;
//etc

Share this post


Link to post
Share on other sites
JohnBolton    1372
Quote:
Original post by Nice Coder
Perhaps writing a binary helper function would help?

x = binaryfunction("10110101");


Almost. Use the STL bitset type. For example:
    std::bitset<8> b = "10101010"; 

Share this post


Link to post
Share on other sites
supercoder74    154
Quote:
Original post by JohnBolton
Quote:
Original post by Nice Coder
Perhaps writing a binary helper function would help?

x = binaryfunction("10110101");


Almost. Use the STL bitset type. For example:
    std::bitset<8> b = "10101010"; 


agreed.
Bitset was made for this type of thing.
EDIT:you could use bit_vector too.
example:
bit_vector bits(numofbits);
bits[0]=true;//or bits[0]=1;
bits[1]=false;//or bits[1]=0;
...
bits[9]=false;
but I strongly suggest using bitset because of more operators overloaded, eiser to use, ect.

Share this post


Link to post
Share on other sites
RuneLancer    253
Quote:
Original post by Icefox
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.


I was going to reply that, but I got beaten to it. ;) Quoted for truth though. What's more readable?

PlayerStatus = PS_POISONED | PS_ALIVE | PS_MBARRIER;
PlayerStatus = 10010010;

The only reason you'd want to "PlayerStatus = PlayerStatus | 00010000;" is so you can act all l33t and hardcore around newbie programmers. ;) Your code will be a mess and you'll hate yourself for it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this