Return the result of swapping the two lower-order bytes of X
pProblem and solution:/p precode/** Return the result of swapping the two
lower-order bytes of X. * For example, if X is 0x12345678, then swap(X) is
0x12347856. */ static int swapLower(int X) { /* Solution */ int lower = X
amp; 0x0000ffff; int upper = X amp; 0xffff0000; return upper | (0xffff
amp; ((lower lt;lt; 8) | (lower gt;gt; 8))); } /code/pre pI am confused
about how to understand the solution. I tried working through the logic
but I did not understand./p pAlso, I don't know how to come up with the
solution in the first place!/p pEDIT:/p pstrongProperties:/strong/p
precodex amp; 1 = x x amp; 0 = 0 x | 1 = 1 x | 0 = x /code/pre pcodeint
lower = X amp; 0x0000ffff = X amp; 0b0000000000000000111111111111/code.
Therefore, codeint lower =/code 0000000000000000xsub15/sub ...
xsub0/sub./p pcodeint upper = X amp; 0xffff0000 = X amp;
0b1111111111110000000000000000/code. Therefore, codeint upper =/code
xsub31/sub ... xsub16/sub0000000000000000./p pstronglower lt;lt; 8/strong
= 0000000000000000xsub15/sub ... xsub0/sub lt;lt; 8 =
00000000xsub23/sub...xsub8/sub00000000/p pstronglower 8/strong =
0000000000000000xsub15/sub ... xsub0/sub 8 =
0000000000000000xsub15/sub...xsub8/sub/p pstrong(lower lt;lt; 8) | (lower
8)/strong = 00000000xsub23/sub...xsub8/sub00000000 |
0000000000000000xsub15/sub...xsub8/sub/p
No comments:
Post a Comment