C++ Logo

std-proposals

Advanced search

Re: Extending std::bitset

From: JeanHeyd Meneide <phdofthehouse_at_[hidden]>
Date: Thu, 12 Mar 2020 16:11:03 -0400
A better direction: https://thephd.github.io/concerns-sg6-numerics-ts

On Thu, Mar 12, 2020 at 4:00 PM Jake Arkinstall via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> I think it's more about the code, when a large binary representation unnecessarily gets in the way compared with something a quarter of the size.
>
> strtoull and co have base arguments, so having the same thing for a bitset makes some sense IMO.
>
> On Thu, 12 Mar 2020, 19:53 Jorg Brown via Std-Proposals, <std-proposals_at_[hidden]> wrote:
>>
>> Ramy -
>>
>> You're saying that using strings to initialize bitsets is infeasible because of their size. To me, this sounds like you're concerned about, in this case, 80 bytes of RAM?
>>
>> You go on to suggest that the bitset string constructor should accept another argument, indicating the base of the string, and that the to_stirng member function should accept a base.
>>
>> But this also infeasible, albeit for a different reason: you're now suggesting that the standard library should be able to perform an arbitrary-precision conversion of a string from one base to another.
>>
>> That would cost a lot more than 80 bytes of code... and std::bitset is an odd place to put that conversion operation.
>>
>> I believe that, for your needs, it should be easy to write a pair of functions:
>>
>> template<size_t N>
>> std::bitset<N> bitset_from_hex(std::string_view);
>>
>> template<size_t N>
>> std::string hex_from_bitset(const std::bitset<N>&);
>>
>> Does that work for you?
>>
>> -- Jorg
>>
>>
>> On Thu, Mar 12, 2020 at 12:25 PM ramy koudsi via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>>
>>> Apologies, I must have been referring to an old spec by accident.
>>> C++11 and newer does indeed accept a string in one of the constructors.
>>>
>>> However, the accepted string must be zeroes and ones. This doesn't seem very feasible, especially as we get larger bitsets.
>>> Using current C++11 or newer, to initialize a bitset with a value of 80 bits, we would have to pass an 80 character string.
>>>
>>> For this reason I see a need for the bitset string constructor to accept another argument, indicating the base of the string.
>>> This way, initializing a value can be done with a much smaller string.
>>>
>>> A base can also be passed to the to_string member function, telling it to output a string in that base, instead of just zeroes and ones.
>>>
>>> Thank you,
>>>
>>> Ramy
>>>
>>>
>>> On March 12, 2020 at 6:07 AM, Magnus Fromreide <magfr_at_[hidden]> wrote:
>>>
>>> On Thu, Mar 12, 2020 at 01:49:42AM -0400, ramy koudsi via Std-Proposals wrote:
>>>
>>> Hi,
>>>
>>>
>>> std::bitset does not have a size limit, however it has an initialization size limit. Bitsets cannot currently be initialized with anything bigger than a long long int.
>>>
>>> To remove this limitation I propose adding another constructor that accepts a std::string object.
>>>
>>>
>>> std::bitset already (since c++11) have a constructor accepting a
>>> std::basic_string.
>>>
>>> /MF
>>>
>>> I see its use like:
>>>
>>> std::bitset<80> bigbs(“FFFF95D3FFFFAC27FFFF”);
>>>
>>>
>>> The example above uses hex strings, but can easily be changed to accept strings of any base by adding another constructor accepting a string and an argument showing the base.
>>>
>>> std::bitset<12> littlebs(“AFD”, 16);
>>>
>>>
>>> In addition, a member function to_string(int base) would be nice.
>>>
>>> std::string littlestr = littlebs.to_string(16);
>>>
>>> // littlestr is “AFD”
>>>
>>>
>>> I wanted to hear your thoughts about this. Has anyone thought of this and come up with a better solution?
>>>
>>>
>>> Thank you
>>>
>>>
>>> Ramy
>>>
>>> --
>>>
>>> Std-Proposals mailing list
>>>
>>> Std-Proposals_at_[hidden]
>>>
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-03-12 15:13:59