2

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

In C I can write

uint32_t a = 0x40022000;

using hex. Can I do something similar by entering binary digits?

Community
  • 1
  • 1
Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • `0b100110...` does work? – Iulius Curt Mar 12 '12 at 10:10
  • 1
    Why would you ? If it's flags you are looking to implement, use something like `#define FLAG_ON(a, flag_num) ((a) |= (1 << flag_num))` `#define FLAG_OFF(a, flag_num) ((a) &= ~(1 << flag_num))`, `FLAG_ON(a, 3)` will give you a = binary 00001000 – Eregrith Mar 12 '12 at 10:18

2 Answers2

9

You can't do that with standard C, but some compilers such as gcc supports an extension that allows you to write something like

uint32_t a = 0b11010101110101;
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Don't use non-standard extensions however. They make your code unportable. gcc extensions in particular should be avoided. Unless perhaps if you are writing something intimately associated with the Linux OS, where no other compiler than gcc will ever be used. – Lundin Mar 12 '12 at 10:18
  • 1
    @Lundin: To be fair, clang also supports it. – kennytm Mar 12 '12 at 11:00
  • @lundin: while I mostly agree, it is more important to avoid *especially* MSVC extensions than gcc ones. At least gcc supports a lot of platforms. – Wyatt Ward Feb 01 '16 at 17:49
0

This is not possible. In (ANSI) C you can enter either decimal, hexadecimal or octal digits. Use a converter to convert your binary number to a hex. A standard calculator with scientific capabilities should be enough.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • 2
    Why use a calculator when you're *programming* a giant calculator? Just write a function that converts a string of binary digits to a numeric type. – Caleb Mar 13 '12 at 23:32