Introduction
Programming में हम आमतौर पर numbers पर arithmetic operations करते हैं, जैसे:
- addition (+)
- subtraction (-)
- multiplication (*)
लेकिन computer internally data को binary form (0 और 1) में store करता है।
उदाहरण:
- 5 → 101
- 3 → 011
कई situations में हमें सीधे bits पर operations करने की जरूरत होती है, जैसे:
- low-level programming
- memory manipulation
- flags handling
- embedded systems
इसी purpose के लिए C language में Bitwise Operators दिए गए हैं।
Bitwise operators सीधे binary bits पर काम करते हैं।
Definition
Bitwise Operators वे operators होते हैं जो operands के binary representation पर operation करते हैं।
Types of Bitwise Operators
| Operator | Name |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
1. Bitwise AND (&)
Example
#include <stdio.h>
int main()
{
int a = 5, b = 3;
printf("%d", a & b);
return 0;
}
👉 Binary:
5 = 101
3 = 011
---------
001
Output:
1
Explanation:
- दोनों bits 1 होने पर ही result 1 आता है
2. Bitwise OR (|)
Example
#include <stdio.h>
int main()
{
int a = 5, b = 3;
printf("%d", a | b);
return 0;
}
Binary:
101
011
----
111
Output:
7
Explanation:
- किसी भी bit के 1 होने पर result 1 होता है
3. Bitwise XOR (^)
Example
#include <stdio.h>
int main()
{
int a = 5, b = 3;
printf("%d", a ^ b);
return 0;
}
Binary:
101
011
----
110
Output:
6
Explanation:
- different bits → 1
- same bits → 0
4. Bitwise NOT (~)
Example
#include <stdio.h>
int main()
{
int a = 5;
printf("%d", ~a);
return 0;
}
Output:
-6
Explanation:
- bits invert हो जाते हैं
- 5 → 00000101
- ~5 → 11111010 → -6 (2’s complement)
5. Left Shift (<<)
Example
#include <stdio.h>
int main()
{
int a = 5;
printf("%d", a << 1);
return 0;
}
Binary:
5 = 101
Shift → 1010
Output:
10
Explanation:
- bits left shift → value double हो जाती है
6. Right Shift (>>)
Example
#include <stdio.h>
int main()
{
int a = 5;
printf("%d", a >> 1);
return 0;
}
Binary:
101 → 10
Output:
2
Explanation:
- bits right shift → value half हो जाती है
Important Trick (Exam Useful)
| Operation | Result |
|---|---|
| x << 1 | x × 2 |
| x >> 1 | x ÷ 2 |
Technical Understanding
Bitwise operators का use इन situations में होता है:
- flags handling
- memory optimization
- embedded systems
- low-level programming
Important Points
- Bitwise operators binary level पर काम करते हैं
- &, |, ^ logical bit operations हैं
- <<, >> shifting operators हैं
- ~ complement operator है
Exam Points
- binary conversion practice जरूरी है
- Bitwise operators binary पर काम करते हैं
- AND, OR, XOR बहुत important हैं
- shift operators से output questions आते हैं
- NOT operator tricky होता है