Binary Notation

Started by
12 comments, last by RuneLancer 18 years, 10 months ago
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.
Advertisement
Perhaps writing a binary helper function would help?

x = binaryfunction("10110101");

?

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.
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}


For more info check this article out.
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;
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}
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.
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192
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?

This topic is closed to new replies.

Advertisement