Advertisement
Guest User

ORS vaje 1

a guest
Oct 15th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5. // ResetBit
  6. printf("Result 1: %X\n", ResetBit(0xF, 2));
  7. printf("Result 2: %X\n", ResetBit(0xA, 0));
  8.  
  9. // ResetTwoBits
  10. printf("Result 3: %X\n", ResetTwoBits(0xFF, 3));
  11. printf("Result 4: %X\n", ResetTwoBits(0xB7, 3));
  12.  
  13. // SetBit
  14. printf("Result 5: %X\n", SetBit(0xA, 0));
  15. printf("Result 6: %X\n", SetBit(0xE, 2));
  16.  
  17. // SetTwoBitsTo
  18. printf("Result 7: %X\n", SetTwoBitsTo(0xFF, 3, 1));
  19. printf("Result 8: %X\n", SetTwoBitsTo(0xAF, 3, 2));
  20.  
  21. return 0;
  22. }
  23.  
  24. int ResetBit(int x, int p)
  25. {
  26. return x & ~(1 << p);
  27. }
  28.  
  29. int ResetTwoBits(int x, int p)
  30. {
  31. return x & ~(1 << p) & ~(1 << p+1);
  32. return x & ~(3 << p);
  33. }
  34.  
  35. int SetBit(int x, int p)
  36. {
  37. return x | (1 << p);
  38. }
  39.  
  40. int SetTwoBitsTo(int x, int p, int n)
  41. {
  42. //printf("x je: %X\n", x);
  43. x = ResetTwoBits(x, p);
  44. //printf("po resetu je x: %X\n", x);
  45. if(n % 2 == 1) {
  46. x = SetBit(x, p);
  47. }
  48. if((n/2) % 2 == 1) {
  49. x = SetBit(x, p+1);
  50. }
  51. return x;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement