Page 1 of 1

Suggestion: Binary Integers

Posted: 16 Oct 2018, 14:18
by Ursi
I was kinda surprised when I realized there was no support for binary integers—not even in format. I think a 0b prefix, or at least a b format type, would be great.

Re: Suggestion: Binary Integers

Posted: 16 Oct 2018, 16:03
by jeeswg
- I have said that 0b... would be nice to match 0x... which we already have.
- Is it especially common? I know that Java and Python support it, but not C++.
- I would definitely like something like BaseToDec and DecToBase (in some form). Out of interest, do you actually use 0b... that often? Thanks.

Re: Suggestion: Binary Integers

Posted: 16 Oct 2018, 17:24
by Ursi
I do not use it often, but recently something got me looking for it, only to realize it wasn't there (I don't remember what that thing was). But octal is a format type, is octal used more than binary?

Re: Suggestion: Binary Integers

Posted: 16 Oct 2018, 17:29
by jeeswg
- The Format function accepts octal, good point. Once you add a letter to Format, there's no turning back. So even though I think adding b for binary might be a good idea, I'm cautious. (Dec to/from Base functions, like I mentioned, would definitely be welcome.)
- I use bin, dec, hex regularly, but never oct, if anyone would like to say why octal is useful. Those are the classic 4 that usually have special functions/syntax.
- One time when 0b would have been useful was while I was writing my CRC-32 tutorial.

[EDIT:] 'b' does look like a good candidate for Format.

Re: Suggestion: Binary Integers

Posted: 16 Oct 2018, 21:25
by lexikos
Most of the formatting for each value is done with a C runtime function similar to printf. All of the type characters and flags supported by Format except for U, L and T are implemented by passing them to this function. Octal is supported merely because the Microsoft's printf supports it; on our end it's just a case of adding one letter to the string of acceptable type characters. I'm not aware of any C runtime functions that support binary literals (parsing or formatting), and I am not inclined to write code for it myself since I won't find it useful.

Re: Suggestion: Binary Integers

Posted: 12 May 2019, 21:23
by lexikos
As literals such as 0b0001 and 0o0007 are effectively reserved for future use by virtue of the fact that variable names cannot start with a digit in v2, I believe this could be implemented post-v2.0 without breaking scripts if someone is ever inclined to do so. I have moved the topic to Wish List.

Re: Suggestion: Binary Integers

Posted: 13 May 2019, 03:29
by Helgef
lexikos wrote: ↑
16 Oct 2018, 21:25
I'm not aware of any C runtime functions that support binary literals (parsing or formatting)
_i64tow and _wcstoui64 supports base 2..36.
Spoiler

Re: Suggestion: Binary Integers

Posted: 13 May 2019, 05:15
by lexikos
I've known that for a long time, and was reminded when I wrote tcstoi64_o(), which was months before that post, so I'm not sure why I posted that. Perhaps I was referring to the "0o" and "0b" prefixes, which we would need to recognize before calling those functions (or parsing it ourselves).

It's still simpler if we can just pass the formatting codes and values to sprintf or equivalent. On the other hand, that it does that is why Format's hex mode never prints negative values.