Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x = 1010 0011
- n = 2
- This should produce:
- x = 1110 1000
- Now run through the code:
- mask = ~(~(1 << n) + 1)
- mask = ~(~(0000 0001 << 2) + 1)
- mask = ~(~(0000 0100) + 1)
- mask = ~((1111 1011) + 1)
- mask = ~(1111 1100)
- mask = 0000 0011 (This can be used to save the first two bits, which we want, since shifting right will delete them from x)
- int right = x & mask
- int right = 1010 0011 & 0000 0011
- int right = 0000 0011 (first two bits saved)
- x = (x >> n)
- x = 1010 0011 >> 2
- x = 1110 1000 (The MSB was a 1, so it filled with 1's. Don't worry, it'll be fixed later)
- int a = ~((1 << 31) >> ~(~n + 1))
- int a = ~((0000 0001 << 7) >> ~(~2 + 1))
- int a = ~((0000 0001 << 7) >> ~(~0000 0010 + 1))
- int a = ~(1000 0000 >> ~(1111 1101 + 1))
- int a = ~(1000 0000 >> ~1111 1110)
- int a = ~(1000 0000 >> 0000 0001)
- int a = ~(1100 0000)
- int a = 0011 1111 (this can be used to clear out the first n bits from x)
- x = (x & a)
- x = 1110 1000 & 0011 1111
- x = 0010 1000 (x has now been shifted to the right, and the first n bits were cleared)
- right = right << (8 + (~n + 1));
- right = 0000 0011 << (8 + (~2 + 1));
- right = 0000 0011 << (8 + (~0000 0010 + 1));
- right = 0000 0011 << (8 + (1111 1101 + 1));
- right = 0000 0011 << (8 + (1111 1110));
- right = 0000 0011 << (8 - 2);
- right = 1100 0000
- return(right | x);
- return(1100 0000 | 0010 1000);
- return(1110 1000); (The answer we wanted)
- See how it works?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement