Advertisement
KirillMysnik

Boolean

Nov 6th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. Some boolean logic
  2.  
  3. player.buttons = 000011001000 (2**3 + 2**6 + 2**7 = 200)
  4. PlayerButtons.DUCK = 000010000000 (2**7 = 128)
  5. PlayerButtons.JUMP = 000001000000 (2**6 = 64)
  6. PlayerButtons.USE = 000000001000 (2**3 = 8)
  7. PlayerButtons.SCORE = 010000000000 (2**10 = 1024)
  8. That means that the player is holding DUCK, JUMP and USE.
  9.  
  10. To add to that list SCORE, you should use a | operator:
  11. player.buttons |= 010000000000
  12.  
  13. Now player.buttons equals to:
  14. 010011001000 (2**3 + 2**6 + 2**7 + 2**10 = 1224)
  15.  
  16. To remove DUCK from the list, you can first negate DUCK using ~ operator:
  17. 111101111111
  18.  
  19. Now if you apply it with &=, all bits will stay the same, except one. One bit will be nulled:
  20. player.buttons &= 111101111111
  21.  
  22. Now player.buttons equals to:
  23. 010001001000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement