You know right shift 1 bit means dividing an integer by 2 and You know left shif
ID: 1846134 • Letter: Y
Question
You know right shift 1 bit means dividing an integer by 2 and You know left shift 1 bit means multiplying by 2.
(a) Does this mean right shift is division exactly by 2? Explain this by shifting 0x9A 1 bit, 2 bits and 3 bits and show what values are 0x9A >> 1, 0x9A >> 2 and 0x9A >> 3.
(b) Is it true that right shifting will always generate an integer of smaller or the same magnitude? When will n >> 1 be the same as n?
(c) Given a general nonzero integer n with 0 < n < 255, what is / compute the minimum number of right shifts r to cause n >> r to become 0 (so this means n >> (r - 1) is still nonzero. What is that value?).
(d) You have learned 0x6 << 4 = 0x60. Calculate 0x6 << n for n = 1 up to 8.
(e) Can you conclude that left shift always create a bigger integer (of twice as big)? Why?
(f) When do left shift 0x6 << n generate strange results? (there may be several n in (a) that this happens). Explain why you get odd results for such n.
Explanation / Answer
a) Not Always. If the number is even, its exactly divisble. If not, it gives (n-1)/2 , where n is the given number.
0x9A(154 in int) >> 1 =0x4D (77 in int)
0x9A >> 2 = 0x26 (38 in int) = (77 - 1)/2
0x9A >> 3 = 0x13 (19 in int)
b) Answer is no. If n=0, n>>1 is also 0.
c)Answer is floor(log n) + 1 where log is calculated with base 2.
d)
0x6 << 1 = 0xc = 12
0x6 << 2 = 0x18 = 24
0x6 << 3 = 0x30 = 48
0x6 << 4 = 0x60 = 96
0x6 << 5 = 0xc0 = 192
0x6 << 6 = 0x180 = 384
0x6 << 7 = 0x300 = 768
0x6 << 8 = 0x600 = 1536
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.