简体   繁体   English

如何在不使用位掩码的情况下在整数中设置位

[英]How to set a Bit in an Integer without using a bitmask

I want to set a bit on an integer but without creating a bitmask to help me. 我想在整数上设置一个位,但不创建位掩码来帮助我。

Example: a=128 and masc=00000001 ...a&masc=10000001 示例:a = 128并且masc = 00000001 ... a&masc = 10000001

Example on how to do it with a bitmask: 使用位掩码的示例:

int y,masc;
int a=128;
masc=0x01;
y=a|masc;
cout<<y;

The question now is: Is there a way to do this without the bitmask and if possible shorter then this code? 现在的问题是: 是否有一种方法可以在没有位掩码的情况下做到这一点?

There are a number of different ways of doing this but it's tricky to offer a 'better' solution without knowing why simply using a bit mask is a problem. 有许多不同的方法可以做到这一点,但是要提供“更好”的解决方案却不知道为什么仅使用位掩码会带来问题是很棘手的。 For example, you could use a helper function along the lines of: 例如,您可以按照以下方式使用辅助功能:

template< typename BITS> inline BITS set_bit(BITS data, size_t pos, bool val)
{
    return ( val ) ? data | ( 1 << pos ) : data & ~ ( 1 << pos );
}

unsigned int a = 128;
a = set_bit(a, 0, true);
std::cout << a << std::endl;   // gives 129 = 10000001
a = set_bit(a, 4, true);
std::cout << a << std::endl;   // gives 145 = 10010001
a = set_bit(a, 7, false);
std::cout << a << std::endl;   // gives 17  = 00010001

(Note that I've not added any bounds checking to this on the pos parameter) (请注意,我尚未在pos参数上对此添加任何边界检查)

Another way to do this might be to use a bitfield which allows you to declare variable names for individual bit and groups of bits of a larger type as follows: 另一种方法是使用位域 ,该位域允许您为较大类型的单个位和位组声明变量名称,如下所示:

struct MyBits {
    unsigned int alpha : 1,
                 beta : 1,
                 gamma : 1;
};

MyBits b;
b.alpha = 1;
b.gamma = 1;

std::cout << b.alpha << std::endl;  // gives 1
std::cout << b.beta << std::endl;   // gives 0
std::cout << b.gamma << std::endl;  // gives 1

A third way - although this is not strictly using an int as such - might be to use a bitset to achieve a similar job: 第三种方式-虽然这不是严格使用int这样-可能是使用一个bitset来实现类似的工作:

#include <bitset>

std::bitset<8> c;
c[0] = true;
c[2] = true;

std::cout << c[0] << std::endl; // gives 1
std::cout << c[1] << std::endl; // gives 0
std::cout << c[2] << std::endl; // gives 1

All have their advantages and disadvantages. 都有其优点和缺点。 It largely depends on the motivating factors behind your question. 这很大程度上取决于您提出问题的动机。

In C++ (and computers in general) you have two types of operations: numerical and bit operations. 在C ++(和一般的计算机)中,您有两种类型的运算:数字运算和位运算。 Since you want to avoid the latter, you have to use the former. 由于要避免使用后者,因此必须使用前者。

Luckily there's a simple association between bits and numbers. 幸运的是,位和数字之间存在简单的关联。 Bits in an integer correspond to powers of two. 整数中的位对应于2的幂。 The lowest bit represents 2^0=1, the next one 2^1=2 etcetera. 最低位代表2 ^ 0 = 1,下一位代表2 ^ 1 = 2等。

We need to start with the highest power of two: 我们需要从两个的最高幂开始:

int p2 = (INT_MAX - INT_MAX/2);

and then copy every set bit over: 然后复制每个设置的位:

int result = 0
while (p2 > 0)
{
    if (a>p2) // This bit is set in a2, copy it.
    {
         result += p2;
         a -= p2;
    }
    p2 /= 2;
}

except when p2 is the bit you want to set, of course. 当然,除了要设置p2时。 I'll leave that part of the exercise to you. 我将把练习的那部分留给您。

You can try a macro like this 您可以尝试这样的宏

#define SET_BIT(data, bit) (data) |= 1 << (bit);    

Usage 用法

int num = 0;
SET_BIT(num, 2);    // num = 4

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM