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

  • 關於本站
  • 免責聲明