Integer literal binary syntax?

3,689 views
Skip to first unread message

Risto Saarelma

unread,
Nov 13, 2009, 1:22:33 AM11/13/09
to golang-nuts
Since system programming sometimes deals with sets of bit flags, it
would be nice to have a syntax for integer literals in binary.
Something like this: "0b11000111 == 199".

Ian Lance Taylor

unread,
Nov 13, 2009, 1:56:49 AM11/13/09
to Risto Saarelma, golang-nuts
I've seen this suggested a couple of times. So far we have found hex
and octal to be satisfactory. We'll see.

Ian

Peter Froehlich

unread,
Nov 13, 2009, 7:55:38 PM11/13/09
to golang-nuts
Hi all,
I may be exceedingly dense, but why wouldn't you simply wrap stuff
like that in a function? It's not like it's hard to pull those calls
out of inner loops, is it? (I've really never understood why people
have anything but plain decimal integer literals in their
languages...) The only rough spot would be const declarations I guess?
You probably can't call functions in those?

Cheers,
Peter
--
Peter H. Froehlich <http://www.cs.jhu.edu/~phf/>
Senior Lecturer | Director, Johns Hopkins Gaming Lab

Ian Lance Taylor

unread,
Nov 13, 2009, 9:45:00 PM11/13/09
to Peter Froehlich, golang-nuts
Peter Froehlich <peter.hans...@gmail.com> writes:

> On Fri, Nov 13, 2009 at 1:56 AM, Ian Lance Taylor <ia...@google.com> wrote:
>> Risto Saarelma <rsaa...@gmail.com> writes:
>>
>>> Since system programming sometimes deals with sets of bit flags, it
>>> would be nice to have a syntax for integer literals in binary.
>>> Something like this: "0b11000111 == 199".
>>
>> I've seen this suggested a couple of times.  So far we have found hex
>> and octal to be satisfactory.  We'll see.
>
> I may be exceedingly dense, but why wouldn't you simply wrap stuff
> like that in a function? It's not like it's hard to pull those calls
> out of inner loops, is it? (I've really never understood why people
> have anything but plain decimal integer literals in their
> languages...) The only rough spot would be const declarations I guess?
> You probably can't call functions in those?

I'm not sure I understand the suggestion. Are you asking why people
wouldn't just write BinaryInteger("11000111")?

Ian

Peter Froehlich

unread,
Nov 13, 2009, 9:55:09 PM11/13/09
to Ian Lance Taylor, golang-nuts
Hi all,

On Fri, Nov 13, 2009 at 9:45 PM, Ian Lance Taylor <ia...@google.com> wrote:
> I'm not sure I understand the suggestion.  Are you asking why people
> wouldn't just write BinaryInteger("11000111")?

Exactly. I know, it's "offensive" to the compiler engineer: it's
really not much effort to put some code in the scanner to handle
integer literals in all kinds of notations. I am simply arguing from a
"language purist" point of view, nothing else: Why put all those
notations in the language? What's next, Roman numerals?

You should be happy, for once you have someone on the list arguing to
take features *out* of Go. :-D

ianr

unread,
Nov 19, 2009, 3:14:39 AM11/19/09
to golang-nuts
Hi,

On Nov 13, 8:56 am, Ian Lance Taylor <i...@google.com> wrote:
> > Something like this: "0b11000111 == 199".
>
> I've seen this suggested a couple of times.  So far we have found hex
> and octal to be satisfactory.  We'll see.
>
> Ian

Thank you for considering this, it would be very useful (and cheap to
parse!).

FWIW, in C I resort to the following approach...

#define BIN(b) (\
(((0##b & 7<<0*3) ? 1 : 0) << 0) + \
(((0##b & 7<<1*3) ? 1 : 0) << 1) + \
(((0##b & 7<<2*3) ? 1 : 0) << 2) + \
(((0##b & 7<<3*3) ? 1 : 0) << 3) + \
(((0##b & 7<<4*3) ? 1 : 0) << 4) + \
(((0##b & 7<<5*3) ? 1 : 0) << 5) + \
(((0##b & 7<<6*3) ? 1 : 0) << 6) + \
(((0##b & 7<<7*3) ? 1 : 0) << 7) + 0 )

Special note: This was NOT an argument for the ternary operator ;-)

Ian

Edward Marshall

unread,
Nov 19, 2009, 9:05:03 AM11/19/09
to golang-nuts
On Thu, Nov 19, 2009 at 2:14 AM, ianr <ian....@gmail.com> wrote:
On Nov 13, 8:56 am, Ian Lance Taylor <i...@google.com> wrote:
> I've seen this suggested a couple of times.  So far we have found hex
> and octal to be satisfactory.  We'll see.

Thank you for considering this, it would be very useful (and cheap to
parse!).

Agreed; in one area I deal with personally (embedded automotive; comms, specifically), representing a value as a series of bits makes the most visual sense to the programmer; I might be good at doing bin->hex->dec conversions in my head, but that doesn't mean I don't make mistakes from time to time, so being able to represent these kinds of values visually in their "natural" form would be a boon.

(Ian: I don't think you made the case for a ternary operator, but you definitely made an argument against a preprocessor. ;-)

--
Ed Marshall <e...@logic.net>
Felix qui potuit rerum cognoscere causas.
http://esm.logic.net/

Joseph Stewart

unread,
Nov 19, 2009, 9:22:30 AM11/19/09
to golang-nuts
I'll keep on beating the same old drum and suggest the erlang number representation:

BASE#NUMBER

so something like this works nicely for setting register bits:

x = 2#1000_0001

or the same thing in hex

x = 16#81

I see nowhere in the language spec that '#' has any alternative meanings.

-joe
Reply all
Reply to author
Forward
0 new messages