首页 > 试题广场 >

阅读下列代码,请问hash(16), hash(256)的值

[不定项选择题]
unsigned short hash(unsigned short key)
{
    return (key >> 4) % 256
}
请问hash(16), hash(256)的值分别是:

  • 1,16
  • 8,32
  • 4,16
  • 1,32
Key右移4位,等于除以16,故选A
发表于 2015-06-13 19:26:56 回复(0)
更多回答
while key = 16, that is 0000 0000 0001 0000, then we shift right for 4 bit that is key = 1(0000 0000 0000 0001), then get the mod, it is similar to key&(0000 0000 1111 1111),so the same, we get 1.
while key = 256, that is 0000 0001 0000 0000, then we shift right for 4 bit that is key = 16(0000 0000 0001 0000), then get the mode, similar to key&(0000 0000 1111 1111), so the same, we get 16.

Therefore, answer is A. 
发表于 2016-01-10 20:10:06 回复(0)
[-]. 此题没有写明位移的位数,使用1 2 4推测答案为A
发表于 2015-01-16 11:52:41 回复(0)