logo
  • 教程列表
  • SO官方文档
  • 位操作
    • 切换了一下
    • 检查一下
    • 设置所有位
    • 删除最右边的设置位
    • 设置一下
    • 清理一下
    • 将第 n 位更改为 x
    • 计数位设置
    • 检查整数是否为 2 的幂
    • 位操作应用程序小到大写字母
  1. StackOverflow 文档
  2. C++ 教程
  3. 位操作
  4. 将第 n 位更改为 x

将第 n 位更改为 x

Created: November-22, 2018

C 风格的位操作

// Bit n will be set if x is 1 and cleared if x is 0.
number ^= (-x ^ number) & (1LL << n);

使用 std::bitset

set(n,val) - 将 n 设置为值 val。

std::bitset<5> num(std::string("00100"));
num.set(0,true);  // num is now 00101
num.set(2,false); // num is now 00001
  • 计数位设置
  • 清理一下

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 关于我们
  • 免责声明