Do the following operations using Bitwise operator.
1) Right-Shift for a decimal number from 0 to 2.
2) Left-Shift for a decimal number from 0 to 2.
Input-Output format:
Input consists of a number.
Refer Sample Input/Output for Output format.
Sample Input and Output I:
Enter a number:
2
Right shift by 0: 2
Right shift by 1: 1
Right shift by 2: 0
Left shift by 0: 2
Left shift by 1: 4
Left shift by 2: 8
Sample Input and Output II:
Enter a number:
5
Right shift by 0: 5
Right shift by 1: 2
Right shift by 2: 1
Left shift by 0: 5
Left shift by 1: 10
Left shift by 2: 20
Program Code:
#include<stdio.h>
int main()
{
int a;
printf("Enter a number:\n");
scanf("%d", &a);
printf("Right shift by 0: %d\n", a>>0);
printf("Right shift by 1: %d\n", a>>1);
printf("Right shift by 2: %d\n", a>>2);
printf("Left shift by 0: %d\n", a<<0);
printf("Left shift by 1: %d\n", a<<1);
printf("Left shift by 2: %d\n", a<<2);
return 0;
}
Post A Comment:
0 comments: