The Bit-wise Shift Operators is used to take binary patterns and shift the bits to the left to right or right to left.
These are Two types i.e. i) Left shift (<<)
ii) Right Shift (>>)
Eg : int A=4, B,C;
A=4 => 1 0 0 (Binary Form)
B=A<<2; i.e. A is to be left shifted by 2 bits and store the value in B.
1 | 0 | 0 |
Hence it became,
1 | 0 | 0 | 0 | 0 |
Then the Value of B is :
1 0 0 0 0 = 24 = 16
A=4 => 1 0 0 (Binary Form)
C=A>>1; i.e. A is to be Right shifted by 1 bit and store the value in C.
1 | 0 | 0 |
Hence it became,
1 | 0 |
Then the value of C is :
1 0 = 21 = 2
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int A=4,B,C;
clrscr();
B=A<<2;
C=A>>1;
printf("\nA=%d",A);
printf("\nB=%d",B);
printf("\nC=%d",C);
getch();
}
Output:
A=4
B=16
C=2
C=2
Next Topic : Special Operators
Previous Topic : Bit-wise Logical Operators
No comments:
Post a Comment