Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. x = 1010 0011
  2.  
  3. n = 2
  4.  
  5. This should produce:
  6.  
  7. x = 1110 1000
  8.  
  9. Now run through the code:
  10.  
  11. mask = ~(~(1 << n) + 1)
  12.  
  13. mask = ~(~(0000 0001 << 2) + 1)
  14.  
  15. mask = ~(~(0000 0100) + 1)
  16.  
  17. mask = ~((1111 1011) + 1)
  18.  
  19. mask = ~(1111 1100)
  20.  
  21. mask = 0000 0011 (This can be used to save the first two bits, which we want, since shifting right will delete them from x)
  22.  
  23. int right = x & mask
  24.  
  25. int right = 1010 0011 & 0000 0011
  26.  
  27. int right = 0000 0011 (first two bits saved)
  28.  
  29. x = (x >> n)
  30.  
  31. x = 1010 0011 >> 2
  32.  
  33. x = 1110 1000 (The MSB was a 1, so it filled with 1's. Don't worry, it'll be fixed later)
  34.  
  35. int a = ~((1 << 31) >> ~(~n + 1))
  36.  
  37. int a = ~((0000 0001 << 7) >> ~(~2 + 1))
  38.  
  39. int a = ~((0000 0001 << 7) >> ~(~0000 0010 + 1))
  40.  
  41. int a = ~(1000 0000 >> ~(1111 1101 + 1))
  42.  
  43. int a = ~(1000 0000 >> ~1111 1110)
  44.  
  45. int a = ~(1000 0000 >> 0000 0001)
  46.  
  47. int a = ~(1100 0000)
  48.  
  49. int a = 0011 1111 (this can be used to clear out the first n bits from x)
  50.  
  51. x = (x & a)
  52.  
  53. x = 1110 1000 & 0011 1111
  54.  
  55. x = 0010 1000 (x has now been shifted to the right, and the first n bits were cleared)
  56.  
  57. right = right << (8 + (~n + 1));
  58.  
  59. right = 0000 0011 << (8 + (~2 + 1));
  60.  
  61. right = 0000 0011 << (8 + (~0000 0010 + 1));
  62.  
  63. right = 0000 0011 << (8 + (1111 1101 + 1));
  64.  
  65. right = 0000 0011 << (8 + (1111 1110));
  66.  
  67. right = 0000 0011 << (8 - 2);
  68.  
  69. right = 1100 0000
  70.  
  71. return(right | x);
  72.  
  73. return(1100 0000 | 0010 1000);
  74.  
  75. return(1110 1000); (The answer we wanted)
  76.  
  77. See how it works?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement