将一个整数的位X设为一个没有分支的整数的Y ?

时间:2022-05-29 23:33:53

Can the copy_bit function below be simplified to something like out[out_bit] = in[in_bit]? (i.e. Not using an if statement)

下面的copy_bit函数可以简化为out[out_bit] = in[in_bit]吗?(即不使用if语句)

template< typename T >
inline void copy_bit( T& out, const T in, const std::size_t out_bit, const std::size_t in_bit )
{
    if ( (in & (1 << in_bit)) != 0 )
    {
        out |= (1 << out_bit); // Set bit
    }
    else
    {
        out &= ~(1 << out_bit); // Clear bit
    }
}

// Set bit 4 in x to bit 11 in y
copy_bit( x, y, 4, 11 );

Update: Just to be clear, this isn't homework or an XY problem where suggesting std::bitset answers the question.

更新:澄清一下,这不是家庭作业,也不是XY问题,建议std::bitset回答这个问题。

3 个解决方案

#1


9  

you can do it like that like this:

你可以这样做:

//Change the bit if and only if they are not equal:
out ^= (((out >> out_bit) ^ (in >> in_bit)) & 1) << out_bit;

(Shift both values so that the required bits are in the least significant position with >>, select with & only the lower bit of the result of the ^ operation; then shift the result into position of an otherwise zero-value to ^ with the original destination. The result is the same as copying bit in_bit of in to bit out_bit of out.)

(两种价值观的转变,这样所需的比特与> >在最显著的位置,选择&只有低一点^操作的结果;然后转移结果到位的新鲜感与最初的目的地^。结果与将位in_bit复制到位out_bit一样)。

#2


6  

One way to do so in one line would be to first reset the output bit to zero, and then OR it with whatever bit the in number has:

在一行中做到这一点的一种方法是首先将输出位重置为零,然后再将输出位重新设为零,无论输入位是多少:

(out &= ~(1 << out_bit)) |= (((in >> in_bit) & 1) << out_bit)

#3


4  

Try this:

试试这个:

template< typename T >
inline void copy_bit( T& out, const T in, const std::size_t out_bit, const std::size_t in_bit )
{
    out = (out & ~(1 << out_bit)) | (((in & (1 << in_bit)) >> in_bit) << out_bit);
}

Explanation:

解释:

  • (out & ~(1 << out_bit)) leave the bits of out that aren't interesting.
  • (out & ~(1 < out_bit)))留下不感兴趣的位。
  • (in & (1 << in_bit) select the bit of in that is interesting
  • (in & (1 < in_bit)选择感兴趣的位
  • (((in & (1 << in_bit)) >> in_bit) << out_bit) positionate the bit in the correct position.
  • ((in & (1 < in_bit) >> in_bit) < out_bit)将位定位在正确的位置。

#1


9  

you can do it like that like this:

你可以这样做:

//Change the bit if and only if they are not equal:
out ^= (((out >> out_bit) ^ (in >> in_bit)) & 1) << out_bit;

(Shift both values so that the required bits are in the least significant position with >>, select with & only the lower bit of the result of the ^ operation; then shift the result into position of an otherwise zero-value to ^ with the original destination. The result is the same as copying bit in_bit of in to bit out_bit of out.)

(两种价值观的转变,这样所需的比特与> >在最显著的位置,选择&只有低一点^操作的结果;然后转移结果到位的新鲜感与最初的目的地^。结果与将位in_bit复制到位out_bit一样)。

#2


6  

One way to do so in one line would be to first reset the output bit to zero, and then OR it with whatever bit the in number has:

在一行中做到这一点的一种方法是首先将输出位重置为零,然后再将输出位重新设为零,无论输入位是多少:

(out &= ~(1 << out_bit)) |= (((in >> in_bit) & 1) << out_bit)

#3


4  

Try this:

试试这个:

template< typename T >
inline void copy_bit( T& out, const T in, const std::size_t out_bit, const std::size_t in_bit )
{
    out = (out & ~(1 << out_bit)) | (((in & (1 << in_bit)) >> in_bit) << out_bit);
}

Explanation:

解释:

  • (out & ~(1 << out_bit)) leave the bits of out that aren't interesting.
  • (out & ~(1 < out_bit)))留下不感兴趣的位。
  • (in & (1 << in_bit) select the bit of in that is interesting
  • (in & (1 < in_bit)选择感兴趣的位
  • (((in & (1 << in_bit)) >> in_bit) << out_bit) positionate the bit in the correct position.
  • ((in & (1 < in_bit) >> in_bit) < out_bit)将位定位在正确的位置。