www.digitalmars.com         C & C++   DMDScript  

D - Quick and easy request: binary literals

reply "QUS" <qus go2.pl> writes:
Allright - octals are pretty useless (or aren't they?), but I was always
missing binary literals from C++. Would it break anything if we had binary
literals in D? Sometimes it saves a lot of time!
Aug 06 2003
next sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
Seconded.

Why not 0b01010101?

Sean

"QUS" <qus go2.pl> wrote in message news:bgr5h8$u9m$1 digitaldaemon.com...
 Allright - octals are pretty useless (or aren't they?), but I was always
 missing binary literals from C++. Would it break anything if we had binary
 literals in D? Sometimes it saves a lot of time!

Aug 06 2003
next sibling parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
Thirded!

Why not 0b01010101?

That first zero kind of makes it hard to read though. Charles "Sean L. Palmer" <palmer.sean verizon.net> wrote in message news:bgrch4$157i$1 digitaldaemon.com...
 Seconded.

 Why not 0b01010101?

 Sean

 "QUS" <qus go2.pl> wrote in message news:bgr5h8$u9m$1 digitaldaemon.com...
 Allright - octals are pretty useless (or aren't they?), but I was always
 missing binary literals from C++. Would it break anything if we had


 literals in D? Sometimes it saves a lot of time!


Aug 06 2003
parent Bill Cox <bill viasic.com> writes:
Charles Sanders wrote:
 Thirded!

Sure, make it four. I have to agree with Walter about the usefulness, but it seems so easy to add... Bill
Aug 06 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Sean L. Palmer" <palmer.sean verizon.net> wrote in message
news:bgrch4$157i$1 digitaldaemon.com...
 Seconded.

 Why not 0b01010101?

 Sean

 "QUS" <qus go2.pl> wrote in message news:bgr5h8$u9m$1 digitaldaemon.com...
 Allright - octals are pretty useless (or aren't they?), but I was always
 missing binary literals from C++. Would it break anything if we had


 literals in D? Sometimes it saves a lot of time!


That exact syntax has been supported by Digital Mars C and C++ for 20 years now. It's even supported as a printf format. To my knowledge, nobody has ever used it. The truth is, it's easier to deal with bit patterns in hex. Quick, what is the value of 0b110010000001000? If you're like me, you stick a penpoint on the screen and count the 0's. So much easier to deal with 0x6408. The only time I found binary notation useful was when I was creating data for a bitmapped cursor <g>. I don't see octal used so much anymore, but a lot of legacy code used it because it mapped well onto the PDP-11 instruction set. Lots of people got used to using octal, and it carried forward into hex computers.
Aug 06 2003
next sibling parent reply Ilya Minkov <midiclub 8ung.at> writes:
Walter wrote:

 That exact syntax has been supported by Digital Mars C and C++ for 20 years
 now. It's even supported as a printf format. To my knowledge, nobody has
 ever used it.

Because it's not *standard*, man! It's being avoided for portability! Why do you think people use C++? Because it's *standard*. Whatever makes it into D, shall become standard within D. And thus gradually accepted.
 The truth is, it's easier to deal with bit patterns in hex.

Not always.
 Quick, what is the value of 0b110010000001000? If you're like me, you stick
 a penpoint on the screen and count the 0's. So much easier to deal with
 0x6408. The only time I found binary notation useful was when I was creating
 data for a bitmapped cursor <g>.

Argh. You must allow for an underscore in numbers. It shall also do readability of integers and floating-point numbers a lot of good. 0b_11111000_00011111 BTW, does this bitmask ring the bell? Well, things like that are not at all rare, and i don't want to need a bin-hex table at hand all the time!
 I don't see octal used so much anymore, but a lot of legacy code used it
 because it mapped well onto the PDP-11 instruction set. Lots of people got
 used to using octal, and it carried forward into hex computers.

The current syntax for octals is horrible anyway. Why don't you make it into 0o123? -i.
Aug 06 2003
next sibling parent "Vathix" <vathix dprogramming.com> writes:
"Ilya Minkov" <midiclub 8ung.at> wrote in message
news:bgrkfk$1db9$1 digitaldaemon.com...
 Walter wrote:

 That exact syntax has been supported by Digital Mars C and C++ for 20


 now. It's even supported as a printf format. To my knowledge, nobody has
 ever used it.

Because it's not *standard*, man! It's being avoided for portability! Why do you think people use C++? Because it's *standard*. Whatever makes it into D, shall become standard within D. And thus gradually accepted.

That is true!
 The truth is, it's easier to deal with bit patterns in hex.

Not always.
 Quick, what is the value of 0b110010000001000? If you're like me, you


 a penpoint on the screen and count the 0's. So much easier to deal with
 0x6408. The only time I found binary notation useful was when I was


 data for a bitmapped cursor <g>.

Argh. You must allow for an underscore in numbers. It shall also do readability of integers and floating-point numbers a lot of good. 0b_11111000_00011111

Kind of silly looking, maybe whitespace something like string literals: 0b 11111000 00011111 I can't think of anything off the top of my head that would break with this.
 BTW, does this bitmask ring the bell? Well, things like that are not at
 all rare, and i don't want to need a bin-hex table at hand all the time!

 I don't see octal used so much anymore, but a lot of legacy code used it
 because it mapped well onto the PDP-11 instruction set. Lots of people


 used to using octal, and it carried forward into hex computers.

The current syntax for octals is horrible anyway. Why don't you make it into 0o123? -i.

For my opinion on binary literals: I've learned to live with using hex so it won't really bother me either way, but it does seem like a good idea to add.
Aug 06 2003
prev sibling next sibling parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
 Argh. You must allow for an underscore in numbers. It shall also do
 readability of integers and floating-point numbers a lot of good.

 0b_11111000_00011111

What an amazing and obvious idea. I can't believe no-one's thought of it before. Please can we allow this syntax for all integer literals (of whatever base) in D!?
Aug 06 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Matthew Wilson wrote:
Argh. You must allow for an underscore in numbers. It shall also do
readability of integers and floating-point numbers a lot of good.

0b_11111000_00011111

What an amazing and obvious idea. I can't believe no-one's thought of it before.

See Ada.
Aug 06 2003
parent "Matthew Wilson" <matthew stlsoft.org> writes:
"Burton Radons" <loth users.sourceforge.net> wrote in message
news:bgs98u$20a6$1 digitaldaemon.com...
 Matthew Wilson wrote:
Argh. You must allow for an underscore in numbers. It shall also do
readability of integers and floating-point numbers a lot of good.

0b_11111000_00011111

What an amazing and obvious idea. I can't believe no-one's thought of it before.

See Ada.

Ah! :)
Aug 06 2003
prev sibling next sibling parent reply "Walter" <walter digitalmars.com> writes:
"Ilya Minkov" <midiclub 8ung.at> wrote in message
news:bgrkfk$1db9$1 digitaldaemon.com...
 Walter wrote:
 That exact syntax has been supported by Digital Mars C and C++ for 20


 now. It's even supported as a printf format. To my knowledge, nobody has
 ever used it.

Why do you think people use C++? Because it's *standard*.

PC C/C++ compilers had many non-standard compiler unique extensions that were widely used.
 I don't see octal used so much anymore, but a lot of legacy code used it
 because it mapped well onto the PDP-11 instruction set. Lots of people


 used to using octal, and it carried forward into hex computers.

into 0o123?

Having 01234 mean 668 in C and 1234 in D would be not worth the confusion. I want to avoid changes that would silently and subtly alter the meaning of code that is second nature to many people.
Aug 06 2003
parent Ilya Minkov <midiclub 8ung.at> writes:
Walter wrote:
 PC C/C++ compilers had many non-standard compiler unique extensions that
 were widely used.

If you leave them no choice...
 Having 01234 mean 668 in C and 1234 in D would be not worth the confusion. I
 want to avoid changes that would silently and subtly alter the meaning of
 code that is second nature to many people.

Then why don't you simply forbid numbers which start with a zero? After all, it doesn't have to be silent... -i.
Aug 07 2003
prev sibling parent "Riccardo De Agostini" <riccardo.de.agostini email.it> writes:
"Ilya Minkov" <midiclub 8ung.at> ha scritto nel messaggio
news:bgrkfk$1db9$1 digitaldaemon.com...
 Argh. You must allow for an underscore in numbers. It shall also do
 readability of integers and floating-point numbers a lot of good.

 0b_11111000_00011111

This one *rocks*! Ric
Aug 25 2003
prev sibling next sibling parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Walter" <walter digitalmars.com> wrote in message
news:bgrfj5$18er$1 digitaldaemon.com...
 "Sean L. Palmer" <palmer.sean verizon.net> wrote in message
 news:bgrch4$157i$1 digitaldaemon.com...
 Seconded.

 Why not 0b01010101?

 Sean


 now. It's even supported as a printf format. To my knowledge, nobody has
 ever used it. The truth is, it's easier to deal with bit patterns in hex.

 Quick, what is the value of 0b110010000001000? If you're like me, you

 a penpoint on the screen and count the 0's. So much easier to deal with
 0x6408. The only time I found binary notation useful was when I was

 data for a bitmapped cursor <g>.

 I don't see octal used so much anymore, but a lot of legacy code used it
 because it mapped well onto the PDP-11 instruction set. Lots of people got
 used to using octal, and it carried forward into hex computers.

of paper next to me so I could write bit masks or bit fields values out in hex I can not count. I think most C/Java programmers would understand what 0b0010 was and as pointed out its not standard so ppl would not tend to use it. I've worked on C project where // comments where banned even though we used gcc and mcvc++ both support // in C. octal: before you scrap it, think of unix programmers, I believe that file permisions on unix are octal so some ppl may still want it. what about 0o777 for octal, numbers are 0<letter>(letter|digits) for anything not base10 with x or h for hex, b for binary and o for octal
Aug 07 2003
parent "Walter" <walter digitalmars.com> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bgu39h$n17$1 digitaldaemon.com...
 the amount of times I've written 0x0..0xF out as 0b0000 .. 0b1111 on a

 of paper next to me
 so I could write bit masks or bit fields values out in hex I can not

I think if you try it in D right now, it already works <g>.
 octal: before you scrap it, think of unix programmers, I believe that file
 permisions on unix are octal so some ppl may still want it.
 what about 0o777 for octal,
 numbers are 0<letter>(letter|digits) for anything not base10 with x or h

 hex, b for binary and o for octal

Octal will stay in, and I'll leave it in the C syntax. For those that still want octal, I think they'll want the C syntax they're used to for it.
Aug 07 2003
prev sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
I would use it.

Consider converting a ARGB1555 pixel to ARGB4444 format:  (yes, we actually
have to do this:)

return ((pixel & 0b_1_00000_00000_00000) >> 3) * 0b1111
        | ((pixel & 0b_0_11110_00000_00000) >> 3)
        | ((pixel & 0b_0_00000_11110_00000) >> 2)
        | ((pixel & 0b_0_00000_00000_11110) >> 1);

It gets worse.  There are all kinds of pixel formats.  Change those numbers
above to hex and you really lose the sense of what's going on.  We have to
resort to writing the values twice, once as hex and once in a comment as
binary "documentation".

Don't get me wrong:  I know hex.  I use hex.  I love hex.  Hex isn't always
the right tool, though.  The brain takes time and effort to convert hex to
binary.  If you just write in binary, you see the patterns immediately.

Which is more obvious what is going on?  0xAA or 0b10101010 ?  When what you
are manipulating *is* bits or bit patterns, hex adds a layer of obfuscation.
It's not so bad when the fields are aligned well, but when they're not, it
can be confusing;  deceiving even.

Actually these days, this kind of code is usually found only in tools, not
in the main game so much anymore.  The main game has support hardware to do
most of the pixel pushing, and integer SIMD to do what the GPU hardware
can't do, fast.  The main cpu is not where you want to be doing this kind of
stuff.

Sean

"Walter" <walter digitalmars.com> wrote in message
news:bgrfj5$18er$1 digitaldaemon.com...
 "Sean L. Palmer" <palmer.sean verizon.net> wrote in message
 news:bgrch4$157i$1 digitaldaemon.com...
 Seconded.

 Why not 0b01010101?

 Sean

 "QUS" <qus go2.pl> wrote in message


 Allright - octals are pretty useless (or aren't they?), but I was



 missing binary literals from C++. Would it break anything if we had


 literals in D? Sometimes it saves a lot of time!


That exact syntax has been supported by Digital Mars C and C++ for 20

 now. It's even supported as a printf format. To my knowledge, nobody has
 ever used it. The truth is, it's easier to deal with bit patterns in hex.

 Quick, what is the value of 0b110010000001000? If you're like me, you

 a penpoint on the screen and count the 0's. So much easier to deal with
 0x6408. The only time I found binary notation useful was when I was

 data for a bitmapped cursor <g>.

 I don't see octal used so much anymore, but a lot of legacy code used it
 because it mapped well onto the PDP-11 instruction set. Lots of people got
 used to using octal, and it carried forward into hex computers.

Aug 08 2003
prev sibling parent "Matthew Wilson" <matthew stlsoft.org> writes:
Ohh yes. That's a spiffy idea!

"QUS" <qus go2.pl> wrote in message news:bgr5h8$u9m$1 digitaldaemon.com...
 Allright - octals are pretty useless (or aren't they?), but I was always
 missing binary literals from C++. Would it break anything if we had binary
 literals in D? Sometimes it saves a lot of time!

Aug 06 2003