Interest in a binary_int template for binary literals?

Greetings, Prompted by some discussion on comp.sdt.c++.moderated, I have a small template that allows the user to declare an integer literal in binary. Usage looks like: #include "binary_int.hpp" unsigned int binary = binary_int<1001,0011,1011,0000>::b; The template is written to support up to 64 bits, although my compiler only allows me test up to 32 bits. I intend to use it for programming hardware registers in the language of the hardware folk at my company. The template is only about 150 lines and doesn't really deserve a library, but it could potentially be tucked into another library if it is deemed useful. Thanks. Scott Schurr

Sounds very useful - if only occasionally (in the absence of a binary input option addition to C++0X which I haven't heard proposed - Ada allows ANY base, of course, so C++ is way behind). Can you post reference to discussion and perhaps append the binary_int.hpp file as it sounds quite short? Thanks Paul Paul A Bristow Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB +44 1539 561830 +44 7714 330204 mailto: pbristow@hetp.u-net.com | -----Original Message----- | From: boost-bounces@lists.boost.org | [mailto:boost-bounces@lists.boost.org] On Behalf Of Scott Schurr | Sent: 12 July 2005 23:32 | To: boost@lists.boost.org | Subject: [boost] Interest in a binary_int template for binary | literals? | | Greetings, | | Prompted by some discussion on comp.sdt.c++.moderated, I have a | small template that allows the user to declare an integer literal | in binary. | | Usage looks like: | | #include "binary_int.hpp" | | unsigned int binary = binary_int<1001,0011,1011,0000>::b; | | The template is written to support up to 64 bits, although my | compiler only allows me test up to 32 bits. I intend to use it | for programming hardware registers in the language of the hardware | folk at my company. | | The template is only about 150 lines and doesn't really deserve a | library, but it could potentially be tucked into another library | if it is deemed useful. | | Thanks. | | Scott Schurr | | _______________________________________________ | Unsubscribe & other changes: | http://lists.boost.org/mailman/listinfo.cgi/boost |

Scott Schurr wrote:
Greetings,
Prompted by some discussion on comp.sdt.c++.moderated, I have a small template that allows the user to declare an integer literal in binary.
Usage looks like:
#include "binary_int.hpp"
unsigned int binary = binary_int<1001,0011,1011,0000>::b;
Why is that better than unsigned x = strtoul("1010010001000010000001", 0, 2); ? I believe that 'strtoul' is part of C99. - Volodya

Vladimir Prus wrote:
Why is that better than
unsigned x = strtoul("1010010001000010000001", 0, 2);
? I believe that 'strtoul' is part of C99.
When I've done something similar to the binary template, it was for compile-time calculations, whereas strtoul I assume is a runtime call. Cheers Russell

Russell Hind <rh_gmane@mac.com> writes:
Vladimir Prus wrote:
Why is that better than
unsigned x = strtoul("1010010001000010000001", 0, 2);
? I believe that 'strtoul' is part of C99.
When I've done something similar to the binary template, it was for compile-time calculations, whereas strtoul I assume is a runtime call.
Not to mention that the number is a lot easier to read when divided into nibbles ;-) -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
Not to mention that the number is a lot easier to read when divided into nibbles ;-)
I do like that. My version never did that and I think BCB6 would cope with somewhere between 20 and 24 bits. Splitting it in to nibbles should mean it can easily cope with 32 or 64 bits which would be very useful. Cheers Russell

"Russell Hind" <rh_gmane@mac.com> wrote in message news:db3fhr$ttq$1@sea.gmane.org...
David Abrahams wrote:
Not to mention that the number is a lot easier to read when divided into nibbles ;-)
I do like that. My version never did that and I think BCB6 would cope with somewhere between 20 and 24 bits. Splitting it in to nibbles should mean it can easily cope with 32 or 64 bits which would be very useful.
IIRC, this topic was discussed and prototyped extensively several months ago on c.l.c++ and/or c.l.c++.m. Jeff Flinn
participants (6)
-
David Abrahams
-
Jeff Flinn
-
Paul A Bristow
-
Russell Hind
-
Scott Schurr
-
Vladimir Prus