stoyanmkd

Bit operations

Dec 19th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.64 KB | None | 0 0
  1. &
  2. 001101 //pos = 3
  3. ^
  4. 1 << pos
  5. 001000
  6. ~
  7. 110111
  8. &
  9. 001101
  10. 000101
  11. bit & ~(1 << pos);
  12. ************************************************
  13. |
  14. 001101 //pos = 4
  15. ^
  16. 1 << pos
  17. 010000
  18. |
  19. 001101
  20. 011101
  21. bit | (1 << pos);
  22. ************************************************
  23. ^
  24. 001101 //pos = 3
  25. ^
  26. 1 << pos
  27. 001000
  28. ^
  29. 001101
  30. 000101 // pos = 3
  31. ^
  32. 1 << pos
  33. 001000
  34. ^
  35. 000101
  36. 001101
  37. bit ^ (1 << pos);
  38. ************************************************
  39. //bit is the number, pos is the position to be changed
  40. Change bit from 0 to 1: bit | (1 << pos);
  41. Change bit from 1 to 0: bit & ~(1 << pos);
  42. Invert bit at position, regardless of value: bit ^ (1 << pos);
Advertisement
Add Comment
Please, Sign In to add comment