14

Possible Duplicate:
Can I use a binary literal in C or C++?

I am learning C and i recently found out that we can represent integers in different ways, like that:

(Assuming i has "human-readable" value of 512.) Here are the representations:

Decimal:

int i = 512; 

Octal:

int i = 01000;

Hexadecimal:

int i = 0x200;

In base 2 (or binary representation) 512 is 1000000000. How to write this in C?

Something like int i = 1000000000b? This is funny but unfortunately no C compiler accepts that value.

Community
  • 1
  • 1

4 Answers4

37

The standard describes no way to create "binary literals". However, the latest versions of GCC and Clang support this feature using a syntax similar to the hex syntax, except it's b instead of x:

int foo = 0b00100101;

As stated, using such binary literals locks you out of Visual Studio's C and C++ compilers, so you may want to take care where and how you use them.

C++14 (I know, I know, this isn't C) standardizes support for binary literals.

zneak
  • 134,922
  • 42
  • 253
  • 328
13

You can't do this in C.

But you said you are learning C++. In C++ you can use BOOST_BINARY until C++0x which will allow user-defined literals.

Please note, however, that it is very easy to become comfortable translating hex to binary and back.

For a given binary number, just group the digits in groups of four and learn that

0000 <-> 0  
0001 <-> 1  
0010 <-> 2   
0011 <-> 3
0100 <-> 4
0101 <-> 5
0110 <-> 6
0111 <-> 7
1000 <-> 8
1001 <-> 9
1010 <-> A
1011 <-> B
1100 <-> C
1101 <-> D
1110 <-> E
1111 <-> F

After a few attempts at doing this translation in your head, you will become very comfortable with it. (Of course, you could do the same with octal, but hex is even more compact than octal.)

For your specific example:

1000000000 -> 10 0000 0000 -> 0010 0000 0000 -> 0x200
jason
  • 236,483
  • 35
  • 423
  • 525
  • 1
    thanks, i should print that and stick to my desk. –  Jun 20 '11 at 16:00
  • Yeah ... after a few conversions where you have to look at the paper you'll find you don't need the paper no more. Memorizing the hexadecimal numbers and their conversion to binary / decimal is easy as pie. – pmg Jun 20 '11 at 16:17
8

In C (and I believe C++) there is no binary representation for numbers. However, for the simple number you show, you can use a shortcut

int i = 1 << 9; /* binary 1 followed by 9 binary 0's */
pmg
  • 106,608
  • 13
  • 126
  • 198
  • what about other numbers (not floats, just integers)? is there a tool or a chart that could help me to learn these "shortcuts"? –  Jun 20 '11 at 16:06
  • You can easily write a table of powers of 2 and use it when needed. I think it's more trouble keeping track of the table (if it's on paper) than to memorize a few of those powers: 2^0==1; then 2, 4, 8, 16, 32 ... 2^10 is 1024; 65536 is 2^16, ..., ... (actually you don't want to memorize after 2^16, and you won't usually need more than 2^8) – pmg Jun 20 '11 at 16:09
  • Or learn the bit patterns of the hex (or even octal) digits, and do the translation yourself. – David Thornley Jun 20 '11 at 16:42
  • When shifting be carefull with the operand types: `1<<9` or `1U<<9` is not the same. – Danijel Jan 20 '17 at 08:00
3

You can't do this in standard C—the language does not cater for specifying integer literals in binary.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490