C++ Logo

std-proposals

Advanced search

Re: [std-proposals] More C++ bit manipulation utilities

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Fri, 27 Jun 2025 07:10:49 +0000
Isn't sign_fill just:

//preparation
using u_t = std::make_unsigned_t<T>;
constexpr u_t mask = u_t{1} << (std::numeric_limits<u_t>::digits - 1);

//actual conversion
T filled = ( std::bit_cast<u_t>(test_var) & mask ) ? T{-1} : T{0};


-----Original Message-----
From: Jan Schultke <janschultke_at_googlemail.com>
Sent: Friday, June 27, 2025 08:50
To: Tiago Freire <tmiguelf_at_[hidden]>
Cc: std-proposals_at_[hidden]
Subject: Re: [std-proposals] More C++ bit manipulation utilities

> I’ve used a version of all of these, semi-regularly.
> However, they are so trivial to implement that it has never occurred to me to write a function to do those.

How do you write sign_fill usually then? Do you just use ">> (std::numeric_limits<T>::digits - 1)" in every place where you sign-fill?

> It might be easier to sell these by packaging them with a set of bit
> operations, like a shift out (i.e. left or right shift overflow bits
> goes into a separate integer), mask multiple bits, bit order swaps,
> etc…

I'm not sure what you mean by "mask multiple bits"; that sounds much like bit_mask, which is on the list.

bit_reverse (bit order swaps) is design-approved for C++29 as part of Bit permutations already.

obtaining the shifted-out bits sounds interesting. That's useful for bit-shifting numbers in multi-precision arithmetic.

Received on 2025-06-27 07:10:56