Can I use a binary literal in C or C++?

hamza picture hamza · Apr 10, 2010 · Viewed 377.9k times · Source

I need to work with a binary number.

I tried writing:

const x = 00010000;

But it didn't work.

I know that I can use an hexadecimal number that has the same value as 00010000, but I want to know if there is a type in C++ for binary numbers and if there isn't, is there another solution for my problem?

Answer

qrdl picture qrdl · Apr 10, 2010

If you are using GCC then you can use a GCC extension (which is included in the C++14 standard) for this:

int x = 0b00010000;