Suggestion: Binary Integers

Propose new features and changes
Ursi
Posts: 18
Joined: 16 Oct 2018, 14:11
GitHub: Ursi

Suggestion: Binary Integers

16 Oct 2018, 14:18

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.
User avatar
jeeswg
Posts: 6724
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Suggestion: Binary Integers

16 Oct 2018, 16:03

- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Ursi
Posts: 18
Joined: 16 Oct 2018, 14:11
GitHub: Ursi

Re: Suggestion: Binary Integers

16 Oct 2018, 17:24

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?
User avatar
jeeswg
Posts: 6724
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Suggestion: Binary Integers

16 Oct 2018, 17:29

- 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.
Last edited by jeeswg on 13 May 2019, 04:04, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6613
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Suggestion: Binary Integers

16 Oct 2018, 21:25

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.
lexikos
Posts: 6613
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Suggestion: Binary Integers

12 May 2019, 21:23

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.
Helgef
Posts: 3929
Joined: 17 Jul 2016, 01:02
Contact:

Re: Suggestion: Binary Integers

13 May 2019, 03:29

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
Last edited by Helgef on 14 May 2019, 03:02, edited 1 time in total.
lexikos
Posts: 6613
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Suggestion: Binary Integers

13 May 2019, 05:15

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.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 3 guests