This project is read-only.

Regarding binary and octal literals, a syntax suggestion

Topics: C# Language Design
May 11, 2014 at 2:18 PM
Hi Roslyn lovers,

I'm browsing the C# threads and the number literals in other bases come back often, with some syntax suggestions.
I've read somewhere (and a long time ago) that the C hexadecimal prefix (the 0x) was initially meant to be generic. It was stated that the number before the x was the base, and 0 had a special meaning, which is "current base", the default current base being hexadecimal. So "current base" became "hexadecimal", and the idea behind this was forgotten.
However, it could be simple to bring it back to life and resolve all suggestions at once:
  • binary: 2x1101111
  • octal: 8x755
  • hexa: 16xABCD (as it was initially intented)
  • even decimals: 10x123456789 (right, I'm not sure this is useful :))
  • hexa for compatibility: 0xABCD
Maybe this could be extended to base 32 or base 64 literals (but I'm not sure this is very useful... Actually I'm pretty sure this is useless...).

What do you think of this?
May 11, 2014 at 4:36 PM
The only thing I can remember for sure about the number system prefixes in C is that the octal prefix '0' originated in C's predecessor, B.

I didn't know where the hexadecimal prefix '0x' came from (can't see anything in K & R about it) but had always imagined that it was just a development of the octal prefix with 'x' being chosen because it's a stronger sound than 'h' and is also further away from the hex digits a-f themselves.

However, even if your recollection about its origin is correct, I wouldn't be keen on resurrecting it now.

I think an '0b' prefix for binary is fine and, as I said in the octal number thread, would like to see '0t' or '0o' introduced for octal even if it's only really useful when dealing with legacy code nowadays.
May 13, 2014 at 2:37 AM
Edited May 13, 2014 at 2:38 AM
vulpecula wrote:
I think an '0b' prefix for binary is fine and, as I said in the octal number thread, would like to see '0t' or '0o' introduced for octal even if it's only really useful when dealing with legacy code nowadays.
I don't like 0t; if I saw
#define BUILD_TIME 0157
unsigned int build_time = 0t##BUILD_TIME;
I would interpret the code as setting the build time to one hundred and fifty-seven (the 0t prefix being necessary to prevent 0157 from being interpreted as octal!) rather than one hundred and eleven.

If I had by druthers, the octal prefix would be 0q [many assemblers historically have used q for octal, since even in uppercase form it's visually distinct from O; the lowercase form of q hanging down below the baseline would probably draw more attention than o], and the old-style zero-only prefix would be deprecated. I have no idea what possessed Mr. Gosling to allow 031 as a representation for twenty-five.
May 13, 2014 at 7:34 AM
picrap wrote:
  • even decimals: 10x123456789 (right, I'm not sure this is useful :))
A decimal prefix could be useful in languages where a leading zero is by itself sufficient to specify octal. I probably use octal more than most people but I still think that's an absolutely positively horrible notation which has no reason to appear in any new language.
May 13, 2014 at 2:42 PM
Actually, I don't have any particular love for 0t or 0o even though they are the most obvious choices for octal literals and I believe the latter is used in F#.

I'd be quite happy with 0q instead particularly as the letter q has been used for octal notation in the past.