简体   繁体   English

如何更改两个连续位的无符号整数

[英]How to change unsigned integer two consecutive bits

How to change unsigned integer two consecutive bits by a given value: 00 01 or 10: 如何用给定值00 00或10更改两个连续无符号整数位:

For example: 例如:

    unsigned int n = 165; // 1010 0101
    void changeTwoConsecutiveBits(unsigned int& n, int twoBitsValue, int positionStart)
    {
    assert(twoBitsValue== 0 || twoBitsValue== 1 || twoBitsValue== 2);
    ...
    }

    changeTwoConsecutiveBits(n, 2, 2);
    //n == 1010 [10]01 // [1 0] is the value of twoBitsValue: 2
    assert(n == 169);

n = 196; // 1100 0100
changeTwoConsecutiveBits(n, 1, 3);
//n == 110[0 1]100 // [0 1] is the value of twoBitsValue: 1
assert(n == 204);

Here is some sample code that replace the two bits in a position given by the indicated bits. 这是一些示例代码,这些代码在指示的位给定的位置替换了两位。

Probably need also to check that positionStart is valid position for value type (in this case unsigned int ). 可能还需要检查positionStart是值类型的有效位置(在这种情况下为unsigned int )。

Code: 码:

#include <assert.h>
#include <iostream>

void changeTwoConsecutiveBits(unsigned int& n, int twoBitsValue,
                              int positionStart) {
    assert(twoBitsValue == 0 || twoBitsValue == 1 || twoBitsValue == 2);

    // 0b11 or 3 if binary literal are not supported in compiler
    const unsigned int inverse_mask = ~(0b11 << positionStart);
    n = (n & inverse_mask) | (twoBitsValue << positionStart);
}

int main() {
    unsigned int n = 165; // 1010 0101
    std::cout << "n1 pre: " << n << std::endl;
    changeTwoConsecutiveBits(n, 2, 2);
    // n == 1010 [10]01 // [1 0] is the value of twoBitsValue: 2
    assert(n == 169);
    std::cout << "n1 post: " << n << std::endl;

    n = 196; // 1100 0100
    std::cout << "n2 pre: " << n << std::endl;
    changeTwoConsecutiveBits(n, 1, 3);
    // n == 110[0 1]100 // [0 1] is the value of twoBitsValue: 1
    assert(n == 204);
    std::cout << "n2 post: " << n << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最快的函数,用于在无符号整数中将位设置为1 - Fastest function to set bits to one between two bits in an unsigned integer 如何根据位将有符号整数值转换为无符号整数? - How to turn a signed integer value to unsigned, according to its bits? 如何统一检查位是否设置为无符号整数? - How to collectively check whether bits are set in an unsigned integer? 为什么两个连续的转换会更改一个整数? - Why two consecutive casting change an integer number? 如何安全地比较两个无符号整数计数器? - How to safely compare two unsigned integer counters? 如何将32位有符号整数放入64位无符号整数的高32位? - How to put 32-bit signed integer into higher 32 bits of 64-bit unsigned integer? 如何在 c++ 中将两个无符号字符合并为一个无符号短(16 位) - How to merge two unsigned chars into a single unsigned short(16 bits) in c++ 如何将 32 位无符号整数分配给包含 32 位的位字段 - How to assign a 32-bit unsigned integer to a bit field containing 32 bits C ++有符号整数转换为无符号位数更多 - C++ signed integer conversion to unsigned with more bits C ++:如何将int转换为unsigned long而不更改任何位? - C++: How do I cast an int to an unsigned long and not change any bits?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM