这些不是作业,也不是作业。 我正在准备我的中期考试。我需要有人告诉我哪个是正确的答案,并向我详细说明原因。
[2 marks] Given an unsigned integer n, the operation (n & ~1) will produce
(a) the value n mod 4
(b) n with its least significant bit cleared (i.e. set to 0)
(c) the value 2n
(d) the value n/2
(e) the least significant bit of n
[1 mark] Given an unsigned integer n, the operation (n << 1) will produce
(a) n with its least significant bit cleared (i.e. set to 0)
(b) the value n mod 4
(c) the value n/2
(d) the least significant bit of n
(e) the value 2n
我不知道以下2公式是否有助于您解决问题:
if (c & 0x1 == 0x1)~c = c OR c-1 //odd colour value),
if (c & 0x1 == 0x0)~c = c OR c+1 //even colour value.
答案 0 :(得分:1)
(n & ~1)
n AND (not 1)
(not 1)
生成数字1,所有位都被否定,因此11111....11110b
AND
是按位乘法,因此0*?=0
,1*1=1
,1*u=u
AND
之后用这个号码清除最后一位,剩下的就剩下了 (n << 1)
1b<<1 = 10b = 2
1b<<2 = 100b = 4
1b<<3 = 1000b = 8
5<<1=101b<<1=1010b=10
n<<1
有更多位时,您的变量可以存储然后切断MSB位(n<<1)=(2*n) mod (2^32)
答案 1 :(得分:0)
您可以尝试使用此ideone代码,看看结果是什么
using System;
public class Test
{
public static void Main()
{
UInt32 n;
n = 10245;
Console.WriteLine(String.Format("n & ~1 - n = {0}, result = {1}", n, n & ~1));
Console.WriteLine(String.Format("n << 1 - n = {0}, result = {1}", n, n << 1));
}
}
解释为什么依赖于了解bitwise operators defined如何在binary representation中打印输入和输出数字是应该解释事物的视角