C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::big_int

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 2 Apr 2026 12:11:11 +0100
On Thursday, April 2, 2026, Jan Schultke wrote:

>
>
> - It is possible to implement abs like (x < 0 ? -x : x) without
> dynamic allocation. Negation is just creating a reference-counted copy with
> the sign bit flipped.
>
>
Store the sign in with the counter. If the counter's positive, the number's
positive. If negative, negative.

class CounterWithSign {
    intptr_t n; // signed
public:
    CounterWithSign &operator++(void) noexcept
    {
        if ( n < 0 ) --n else ++n;
        return *this;
    }
    CounterWithSign &operator--(void) noexcept
    {
        if ( n < 0 ) ++n else --n;
        return *this;
    }
    operator size_t(void) noexcept
    {
        if ( n < 0 ) return -n else return n;
    }
    bool sign(void) noexcept
    {
        return n >= 0;
    }
};

Received on 2026-04-02 11:11:16