Advertisement
vencinachev

Bitwise masks

Mar 7th, 2022
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. void setZero(int* num){
  4.     *num = 0;
  5. }
  6.  
  7. void swap(int* a, int* b){
  8.     int temp = *a;
  9.     *a = *b;
  10.     *b = temp;
  11. }
  12.  
  13. void setBit(uint32_t* mask, int pos){
  14.     *mask |= (1 << pos);
  15. }
  16.  
  17. void clearBit(uint32_t* mask, int pos){
  18.     *mask &= ~(1 << pos);
  19. }
  20.  
  21. void changeBit(uint32_t* mask, int pos){
  22.     *mask ^= (1 << pos);
  23. }
  24.  
  25. int checkBit(uint32_t mask, int pos){
  26.     return !!(mask & (1 << pos));
  27. }
  28.  
  29. int main(){
  30.     uint32_t mask = 0;
  31.     setBit(&mask, 2);
  32.     clearBit(&mask, 2);
  33.     printf("%d\n", mask);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement