10 years, 2 months ago.

Specify an integer constant in binary

How do I specify an integer constant in binary:

int x = 3; works fine int x = 0xAB; also OK

BUT

int x = 0b011; not OK

Generates error: Compiler Error 65 Expected a ";"

I've looked at Handbook > C Data Types (http://mbed.org/handbook/C-Data-Types) and everywhere else I can think of with no answer. I realize this is a C question, but gcc compiles the above without a problem.

[an aside: (and this is my _real_ question) why is such a simple thing SO HARD? I.E., it took me less time to unpack my device, change the firmware, learn how to use the development system, modify the example code, download my code, and run said code than I've spent looking for the answer to this question!!! (I concede that the mbed system is SO GOOD that it made such hard thing easy, BUT I'm an old perl hacker and have a motto of "easy things easy, hard things possible". (I'm not blaming perl :-)]

Indeed, the answer is easy... int x = 011; Found by catching pop-up box on values for types and trial.

posted by Alan Poindexter 05 Sep 2014

DA, the above of course sets x to eleven.

posted by Alan Poindexter 05 Sep 2014

1 Answer

10 years, 2 months ago.

I'm afraid binary literals are not a part of the c++ specification, some compilers have it as an extension. Hex was preferred as its directly comparable and shorter.

Accepted Answer