Question
|
Difficulty Level:
1
|
Solution
|
Explanation Quality:4
|
This question is very easy if you have worked with bit shiftings. The basic idea
is to write out the binary version of the powers of two and find out a pattern.
Take a look at the following:
|
Decimal
|
Binary
|
|
20
|
0001
|
|
21
|
0010
|
|
22
|
0100
|
|
23
|
1000
|
The pattern is clear from here that in order to get the immediately next higher
power of two we simply perform a left shift. A left shift is that the leftmost bit
is dropped and a zero is added to the rightmost place while shifting all other bits
over to the left by one place.
For the second par of the question, we perform the same trick. Note that in order
to get the result after multiplying any number by two we simple left shift it's
bit by one place. Here's an example
|
Decimal
|
Binary
|
2*Decimal
|
2*Decimal in Binary
|
|
7
|
111
|
14
|
1110
|
|
6
|
110
|
12
|
1100
|
I don't think the knowing this nifty trick to multiply any number by 2 is really
fair question because you would never know it unless you really worked with bit
shiftings or assembly language. Anyhow I can expect such questions at companies
like nVidia.
|