Run the below code and shift binary left and right and understand the output.
#include <stdio.h>
int main()
{
int num=212, i;
for (i=0; i<=2; i++)
printf("Right shift by %d: %d\n", i, num>>i);
printf("\n");
for (i=0; i<=2; i++)
printf("Left shift by %d: %d\n", i, num<<i);
return 0;
}
Output: |
Output :
Right shift by 0: 212
Right shift by 1: 106
Right shift by 2: 53
Left shift by 0: 212
Left shift by 1: 424
Left shift by 2: 848
Explanation :
At first the for loop will iterates from the 0 to 2:
At first i = 0
num >> i = 212 >> 0 (212 / 20)
= 212
i = 1 :
num >> i = 212 >> 1 (212 / 21)
= 106
i = 2 :
num >> i = 212 >> 2 (212 / 22)
= 53
The second loop will starts executing from i = 0 to 2 (Left shift operations)
At first i = 0
num >> i = 212 >> 0 (212 * 20)
= 212
i = 1 :
num >> i = 212 >> 1 (212 * 21)
= 424
i = 2 :
num >> i = 212 >> 2 (212 * 22)
= 8484
Get Answers For Free
Most questions answered within 1 hours.