设置/取消设置单个位的简单方法

发布于 2024-09-13 04:12:42 字数 235 浏览 4 评论 0原文

现在我用它来设置/取消设置一个字节中的各个位:

if (bit4Set)
   nbyte |= (1 << 4);
else
   nbyte &= ~(1 << 4);

但是,你不能以更简单/优雅的方式做到这一点吗?喜欢在一次操作中设置或取消设置该位吗?

注意:我知道我可以编写一个函数来做到这一点,我只是想知道我是否不会重新发明轮子。

Right now I'm using this to set/unset individual bits in a byte:

if (bit4Set)
   nbyte |= (1 << 4);
else
   nbyte &= ~(1 << 4);

But, can't you do that in a more simple/elegant way? Like setting or unsetting the bit in a single operation?

Note: I understand I can just write a function to do that, I'm just wondering if I won't be reinventing the wheel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

终陌 2024-09-20 04:12:42

当然!如果在代码中扩展 |=&= 会更明显,但你可以这样写:

nbyte = (nbyte & ~(1<<4)) | (bit4Set<<4);

注意 bit4Set 必须是零或(不是任何非零值)才能起作用。

Sure! It would be more obvious if you expanded the |= and &= in your code, but you can write:

nbyte = (nbyte & ~(1<<4)) | (bit4Set<<4);

Note that bit4Set must be zero or one —not any nonzero value— for this to work.

浅语花开 2024-09-20 04:12:42

将其放入函数中,bool 类型将为所有 bitval 输入强制执行 0,1。

int change_bit(int val, int num, bool bitval)
{
    return (val & ~(1<<num)) | (bitval << num);
}

Put it in a function, the bool type will enforce 0,1 for all bitval inputs.

int change_bit(int val, int num, bool bitval)
{
    return (val & ~(1<<num)) | (bitval << num);
}
乙白 2024-09-20 04:12:42

这是一个非常明智且完全标准的习语。

This is a perfectly sensible and completely standard idiom.

梦幻之岛 2024-09-20 04:12:42

您是否考虑过为您的位分配助记符和/或标识符,而不是通过数字引用它们?

举个例子,假设设置位 4 启动核反应堆 SCRAM。我们不将其称为“位 4”,而是将其称为 INITIATE_SCRAM。其代码可能如下所示:

int const INITIATE_SCRAM = 0x10; // 1 << 4

...

if (initiateScram) {
    nbyte |= INITIATE_SCRAM;
} else {
    nbyte &= ~INITIATE_SCRAM;
}

这不一定比原始代码更有效(优化后),但我认为它更清晰,并且可能更易于维护。

Have you considered assigning mnemonics and/or identifiers to your bits, rather than referring to them by number?

As an example, let's say setting bit 4 initiates a nuclear reactor SCRAM. Instead of referring to it as "bit 4" we'll call it INITIATE_SCRAM. Here's how the code for this might look:

int const INITIATE_SCRAM = 0x10; // 1 << 4

...

if (initiateScram) {
    nbyte |= INITIATE_SCRAM;
} else {
    nbyte &= ~INITIATE_SCRAM;
}

This won't necessarily be any more efficient (after optimization) than your original code, but it's a little clearer, I think, and probably more maintainable.

初雪 2024-09-20 04:12:42
nbyte |= (1 << 4);

如果赋值的右侧 (1 << 4) 始终是这样的常量,那么编译器可能会对此进行优化,因此生成的汇编结果会更简单:

mov r0, _nbyte
mov r1, 10H         ; here is the optimization, no bit shift occured
or r0, r1
st _nbyte, r0
nbyte |= (1 << 4);

If the right hand side of the assignment, (1 << 4), is always a constant like this, then this would probably be optimized by compiler so it will be simpler in resulting assembly:

mov r0, _nbyte
mov r1, 10H         ; here is the optimization, no bit shift occured
or r0, r1
st _nbyte, r0
油饼 2024-09-20 04:12:42

它被标记为 C++,所以您是否考虑过使用 std::bitset 而不是自己进行所有位操作?然后您可以使用数组表示法:bits[3] = bit4Set 来设置适当的位。

This is tagged as C++ so have you considered using std::bitset instead of doing all the bit manipulation yourself? Then you can just use array notation as: bits[3] = bit4Set to set the appropriate bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文