In the c-based (or c-inspired) family of languages (i.e. C/C++/Objective-C/JavaScript) when typing a constant number, you can simply type the number directly for its decimal value, or you can prefix it with 0x to write it via hexadecimal. JavaScript lets you even type values using Octal encoding by prefixing the number with a zero (…which is completely bonkers to me since 09 looks to be 9 but actually parses to zero! Way to confuse people! Should’ve been 8x prefix or similar, but I digress…)
What I’m wondering is if there’s an equivalent for typing things directly in binary.
For instance, I’d love to see something like this:
int x = Bx00001001; // x would equal 9, (bits 8 and 1 are set)
int x = 1x00001001; // Alternate prefix '1x' instead of 'Bx'
Just for fun, I’m actually considering writing a pre-pre-compiler to do exactly that, but again just for fun since it of course the code wouldn’t be portable.
Still, hoping there’s a standards-way to do this. Granted, yes, it’s very easy to just encode in hex (you only need to learn up to 15 after all), but again, just wondering more out of curiosity.
But really… what the heck is up with that Octal nonsense!! Crazy if you ask me!
There is no binary constants in Standard C.
Standard C only has decimal (no prefix), octal (
0prefix) and hexadecimal (0xor0Xprefix) constants. GNU C provides binary constants as a GNU extension to C, see:6.60 Binary constants using the `0b’ prefix
http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html