Bitwise Operator : Examples in c language in hindi , बिट वाइज ऑपरेटर के उदाहरण c भाषा में हिंदी में

बिट वाइज ऑपरेटर के उदाहरण c भाषा में हिंदी में Bitwise Operator : Examples in c language in hindi :-
इससे पहले के article मे ,हमने bitwise operators को पढ़ा | C language मे ,छ प्रकार के bitwise operator होते है |जैसे
1. Bitwise logical and operator : ये operator logical and logic की तरह कार्य करता है लेकिन ये बिट पर लगते है |अगर दोनों bits ‘1’ है तब ही आउटपुट ‘1’ होगा अन्यथा आउटपुट बिट ‘0’ होगी |
2. Bitwise logical OR operator : ये operator logical OR logic की तरह कार्य करता है लेकिन ये बिट पर लगते है |अगर कोई भी एक bit ‘1’ है तब आउटपुट ‘1’ होगा अन्यथा आउटपुट बिट ‘0’ होगी |
3 .Bitwise logical XOR operator : ये operator logical XOR logic की तरह कार्य करता है लेकिन ये बिट पर applicable होते है |अगर दोनों bits सामान होती  है तब ही आउटपुट ‘1’ होगा अन्यथा आउटपुट बिट ‘0’ होगी |
4. Bitwise Shift operator : इस operator से bits को left या right side मे shift करने के लिए किया जाता है |इसका syntax है >> left shift operator और << right shift operator |
5. compliment Operator : इस operator का use , bits को invert करने के लिए किया जाता है |इसका syntax  “~” है |
 
इस article मे bits wise operator पर based कुछ प्रोग्राम का अध्धयन करेगे |जिससे की bitwise operator को समजा  जा सके |
उदाहरन 1-:
/* Programme for Swapping using Bitwise operator */
इस प्रॉब्लम मे, दो integer डिजिट को swap करना है | Swapping का मतलब है variable ‘a’ और ‘b’ की value को interchange करना |जैसे
input
a=121
b=234
After Swapping
a=234
b=121
Explanation 
Swapping को हमने कई बार पढ़ा है |Swapping के लिए third variable ‘temp’ को use किया जाता है लेकिन bitwise operator के लिए हम XOR operator को use करते हैXOR operator जब अलग अलग होते तब ही ‘1’ आउटपुट आता है | इसका syntax होता है :
o=x^y;
लेकिन जब आउटपुट ‘o’ का किसी एक input से XOR operation perform करते है तब आउटपुट मे दूसरा operand मिलता है जैसे
o ^ x है तब आउटपुट ‘y’ होगा |
o ^ y है तब आउटपुट ‘x’ होगा |
Source Code :
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,o;
printf(“Inputs :”);
scanf(“%d %d”,&x,&y);
printf(“Inpux=%d y=%d”,x,y)
o=x^y;
x=o^x;
y=o^y;
printf(“Output after swaping x=%d  y=%d “,x,y);
getch();
}
Output :
x=21  bit pattern : 010101
y=54  bit pattern : 110110
x^y : bit pattern  : 100011
x=o^x : bit pattern  : 110110 = bit pattern of y = 54
y=o^y : bit pattern  : 010101 = bit pattern of x = 21
उदहारण -2
/* Programme for count zeros in binary number using Bitwise operator */
इस प्रॉब्लम मे, किसी integer के बिट pattern मे , numbers ऑफ़ zeros को count करता है |
a=178
Number of Zeros 5
Explanation 
इस operation के लिए bitwise AND operator को use करते है| जब दोनों बिट ‘1’ होगी तब आउटपुट bit ‘1’ होगा अन्यथा ‘0’ होगा |
इसमें condition check होती| integer के बिट और ‘1’ के बीच and operation होता |अगर आउटपुट ‘0’ है तब count की value set होती है |अन्यथा count की value मे कोई effect नहीं होता है |
Source Code :
#include<stdio.h>
#include<conio.h>
#define size sizeof(int)*8 // macro define जो की size को size of integer मे convert करता है | //
void main()
{
int x,i;
printf(“Inputs :”);
scanf(“%d %d”,&x);
while(i=0;i<size;i++)
{
if(((x>>i) & 1)== 0 )
{
count ++;
printf(“bit is zero”);
}
printf(“Bit is not zero /t Next bit check”);
}
printf(“Total Number of Zero = “,x,y);
getch();
}
Output :
x=21  bit pattern : 010101
bit is zero  यहा पर count=1
Bit is not zero  Next bit check
bit is zero यहा पर count=2
Bit is not zero  Next bit check
bit is zero यहा पर count=3
Bit is not zero  Next bit check
Total Number of Zero = 3
उदहारण -3
/* Programme for rotating a binary number using Bitwise operator */
इस प्रॉब्लम मे, किसी integer के बिट pattern को rotate करने के लिए किया जाता है |
a=78
Number of Rotation 2
Direction left
Rotating Number = 4
Explanation 
इस operation के लिए bitwise shift operator को use करते है| इसमें किसी integer के बिट पैटर्न मे बिट को left और right direction मे shift करते है |
इसमें यूजर द्वारा द्वारा तीन input लेते है (i) integer  (ii) number of rotation (iii) rotation direction |अगर यूजर left direction choose करता है तब left_shift function call होता है और right choose करता है तब function right_shift call होगा |
left_shift () मे ,
अगर most significant bit ‘1’ है तब shift operation bके बाद vacant हुई space पर ‘1’ आ जायेगा |और अगर most significant bit ‘0’ पर है tab vacant space पर zero insert हो जायेगा |सबसे पहले ,बिट पैटर्न मे से,MSB को remove_bit मे सेव करा देते है |फिर shifting perform होगी |बाद मे ,shifting output का MSB के साथ OR operation पेरफोर्म होगा |
right_shift() मे ,
अगर most significant bit ‘1’ है तब shift operation bके बाद vacant हुई space पर ‘1’ आ जायेगा |और अगर most significant bit ‘0’ पर है तब vacant space पर zero insert हो जायेगा |सबसे पहले ,बिट पैटर्न मे से,MSB को remove_bit मे सेव करा देते है |फिर right shifting perform होगी |बाद मे ,shifting output का MSB के साथ OR operation पेरफोर्म होगा
Source Code :
#include<stdio.h>
#include<conio.h>
void left_shift(int ,int );
void right_shift(int , int );
#define SIZE sizeof(int)*8 // macro define जो की size को size of integer मे convert करता है | //
#define  BITS  SIZE*8-1
void main()
{
int x,r;
char choice;
printf(“Input :”);
scanf(“%d “,&x);
printf(“Number of Rotation :”);
scnaf(“%d”,&r);
printf(“For right shifting ,enter ‘r’ or For left shifting ,enter ‘l’ \n
Enter your choice : “);
scanf(“%c”,&choice);
switch(choice)
{
case ‘r’  : right_shift( x,r);
break;
case ‘l’  : left_shift( x,r);
break;
default : printf(“Invalid choice “);
}
getch();
}
void left_shift(int num ,int rotation )
{
int remove_bit;
rotation=rotation%BITS;
while (rotation –)
{
remove_bit = (num>>BITS) & 1;
num=(num<<1)| remove_bit;
}
printf(“Output =%d”,num);
}
void right_shift(int num ,int rotation )
{
int remove_bit;
rotation=rotation%BITS;
while (rotation –)
{
remove_bit = num & 1;
num=(num>>1) & (~(1<<BITS));
}
printf(“Output =%d”,num);
}
Output :
Inputs : 43
Number of Rotation : 2
For right shifting ,enter ‘r’ or For left shifting ,enter ‘l’
Enter your choice : r
Output =58