|
Boost : |
From: Scott Schurr (scott_schurr_at_[hidden])
Date: 2005-07-14 13:49:52
Greetings,
David Abrahams wrote:
> Scott Schurr <scott_schurr_at_[hidden]> writes:
>
>>> * How does it deal with bit values like 0111? This is octal value.
>>
>> Right. There are specilizations for decimal and octal values.
>> This means there is some aliasing. A few non-binary decimal and
>> octal values leak through. For example 0111 (octal) is 73 decimal
>> and, for that matter, 0x49 hex. So the template accepts all of
>> these values. It's not perfect, but it's the best I could figure
>> out.
>
> I think the answer is confusing. I don't know what "aliasing" means
> in this case, but: aside from zero, the decimal numerals containing
> all 1s and 0s have distinct values from the octal numerals containing
> all 1s and 0s, at least up to something like 16 bits. There's no
> ambiguity.
Yes, my answer was confusing. Sorry. Let me try again. The
template operates on values that look as though they should be
binary, but are expressed in decimal or octal. For example:
o 1001 - is a decimal value of one thousand and one, or 0x3E9.
The template transforms this value to decimal 9, or 0x9.
o 0010 - is an octal value that represents the decimal value
eight, or 0x8. The template transforms this value
to decimal 2, or 0x2.
For legitimate binary representations like these, in decimal and
octal, there is never any ambiguity of interpretation. I don't
have a general proof, but the template only relies on no
ambiguity for up to four digits. I have, in effect, an exhaustive
proof in the code that there is no ambiguity up to four digits.
My confusing reference to 'aliasing' could also be called spoofing.
The template can be intentionally misused with certain magic
values. For example, it interprets all of the following numeric
values identically:
1001 == 0x3E9 == 01751 // the template converts all of these
// (equivalent) values to decimal nine.
Therefore...
assert(binary_int<1001>::b == binary_int<01751>::b);
would both compile and return true. But clearly, someone
typing a value other than 1 or 0 in this template is misusing
the template.
The vast majority of numeric values that don't look like binary
are rejected by the template and don't compile.
I don't personally think the 'aliasing' aspect is any cause for
alarm. If you use the template as intended it works well. And
it is resistant to most misuses, just not all of them.
Scott Schurr
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk