krot

set bit

Oct 2nd, 2019
685
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. +Setting a bit
  2. number |= 1 << n;
  3. That will set a bit n.
  4. +Clearing a bit
  5. number &= ~(1 << n);
  6. +Toggling a bit
  7. number ^= 1 << n;
  8. +Checking a bit
  9. bit = (number >> n) & 1;
  10. +Changing the nth bit to x
  11. number ^= (-x ^ number) & (1 << n);
  12. number = number & ~(1 << n) | (x << n);
  13. std::bitset<8> x;
  14. x[1] = 1;
  15. x[2] = 0;
  16.  
  17.     bit_set(<<D/integer>>,Pos,0) ->  <<(D band bnot(1 bsl Pos))/integer>>;
  18.     bit_set(<<D/integer>>,Pos,Val) -> <<(D bor (1 bsl Pos))/integer>>.
  19.  
  20. bit_set(<<D/unsigned>>,Pos,Val) -><<(    D band bnot(1 bsl Pos) bor (Val bsl Pos)  )/unsigned>>.
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×