r***6 发帖数: 15 | 1 Question 1.
Given a variable guaranteed to contain either the value 1 or the value 2,
write a piece of code to make the variable equal to 1 if it contains 2, and
to make it equal to 2 if it contains 1.
Your code should be as fast and as short as possible.
Question 2.
Assuming that total and ip are declared correctly, what is wrong with the
following piece of code?
total = ip->size + (ip++)->size;
Please give your reasoning, and suggest an alternative, correct version. | a****o 发帖数: 15 | 2 Here is my thought for the first problem.
Assume the variable is m.
Then m = m xor 3
and
【在 r***6 的大作中提到】 : Question 1. : Given a variable guaranteed to contain either the value 1 or the value 2, : write a piece of code to make the variable equal to 1 if it contains 2, and : to make it equal to 2 if it contains 1. : Your code should be as fast and as short as possible. : Question 2. : Assuming that total and ip are declared correctly, what is wrong with the : following piece of code? : total = ip->size + (ip++)->size; : Please give your reasoning, and suggest an alternative, correct version.
| p*****2 发帖数: 21240 | 3 total = (ip++)->size+ip->size; ? | r****t 发帖数: 10904 | 4 1. ^3 或者 var = (var==1)?2:1
2. undefined.
and
【在 r***6 的大作中提到】 : Question 1. : Given a variable guaranteed to contain either the value 1 or the value 2, : write a piece of code to make the variable equal to 1 if it contains 2, and : to make it equal to 2 if it contains 1. : Your code should be as fast and as short as possible. : Question 2. : Assuming that total and ip are declared correctly, what is wrong with the : following piece of code? : total = ip->size + (ip++)->size; : Please give your reasoning, and suggest an alternative, correct version.
| r***6 发帖数: 15 | 5
是说ip++ undefined?
【在 r****t 的大作中提到】 : 1. ^3 或者 var = (var==1)?2:1 : 2. undefined. : : and
| a********m 发帖数: 15480 | 6 for(...) a[i] = var1 + var2 - a[i]; |
|