|
Boost : |
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2005-09-03 00:20:43
On 9/2/05, Paul Mensonides <pmenso57_at_[hidden]> wrote:
> The problem is that there isn't any arbitrary precision arithmetic support
> in
> the pp-lib. If there was, you could do better than that for syntax:
>
> BOOST_BINARY_INT( 1100 0011 1010 )
>
You are under the mistaken impression that the resultant number would have
to be a decimal literal and rely on Boost.Preprocessor functionality for
arithmetic computations. Storing the number as a hex literal makes things
much easier, since each nibble is just a hex digit. All you do is convert
each nibble to hex then concatenate them all together. The fact that the
literal is in hex, it has the downside that it would be more difficult to
use with Boost.Preprocessor operations, but then again, the template form
couldn't do that either. Even still, if you really wanted to use it with
Boost.Preprocessor macros, the time could be taken to make an implementation
which results in a small decimal literal. Here is a fully functional
BOOST_BINARY_INT implementation I put together describing what I mean:
http://www.illegal-immigration.com/Riv/boost/binary_int.hpp
Example usage would be BOOST_BINARY_INT( (1100)(0101) ) to represent the hex
literal 0xC5
It works exactly as I described and can be used at preprocessing time
(though again, note that it is a hex literal). I do not immediately see how
to implement it allowing the syntax you describe (where your nibbles are
simply separated by spaces rather than using a Boost.Preprocessor sequence),
so if you could, please enlighten me as I know you must be more experienced
with the preprocessor (I love Boost.Preprocessor by the way).
Note that compared to the template version, this implementation actually
works at preprocessor time, is even shorter and arguably more elegant in
implementation, you can easily append integer suffixes at the end, it
doesn't allow obvious mistakes such as passing a decimal value of 65, and
since it doesn't deal with complex language features such as templates you
have less to worry about concerning portability. As well, if you wish to use
the number as an MPL style integer constant, all you have to do is pass the
resultant value as an argument to an integer constant template.
As a side note, I noticed while making this that BOOST_PP_SEQ_CAT fails on
sequences of size 1. While obviously no concatenations occur with a sequence
of this size, logically the result should just be the single element, so I
consider this an error in Boost.Preprocessor (any disagreements on that?). I
had to make a workaround to account for a single nibble, here, though the
resultant code is still under 60 lines.
Also note that I only allowed values which are 4 binary digits long. This
was just a design choice as I believe it would be confusing having nibbles
in the middle that have under 4 digits. Of course, I could always make a
special case that only the first nibble is allowed to be under 4 binary
digits if the desire for that functionality exists.
-- -Matt Calabrese
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk