Bitwise Operators in C Language with Examples (C भाषा में बिटवाइज ऑपरेटर्स) Introduction to Bitwise Operators (बिटवाइज ऑपरेटर्स का परिचय)

Bitwise operators work on the individual bits of integer operands and perform bit-by-bit operations. These operators are commonly used in low-level programming, embedded systems, device drivers, and situations where individual bits need to be manipulated.

बिटवाइज ऑपरेटर्स Integer Operands की अलग-अलग Bits पर कार्य करते हैं तथा Bit-by-Bit संक्रिया करते हैं। इनका उपयोग मुख्य रूप से Low-Level Programming, Embedded Systems, Device Drivers तथा Bits को नियंत्रित करने वाले प्रोग्राम में किया जाता है।

Suppose:

A = 12 (Binary: 1100)
B = 5 (Binary: 0101)

माना कि:

A = 12 (बाइनरी: 1100)
B = 5 (बाइनरी: 0101)

Types of Bitwise Operators (बिटवाइज ऑपरेटर्स के प्रकार)

C language provides six commonly used bitwise operators.

C भाषा में मुख्य रूप से छह बिटवाइज ऑपरेटर्स का उपयोग किया जाता है।

OperatorDescription (विवरण)ExampleResult
&Bitwise ANDA & B0100 = 4
|Bitwise ORA | B1101 = 13
^Bitwise XORA ^ B1001 = 9
~Bitwise NOT (One's Complement)~A-13*
<<Left ShiftA << 248
>>Right ShiftA >> 23

The result -13 assumes the usual two's-complement representation of signed integers.

1. Bitwise AND Operator (&)

The Bitwise AND operator compares corresponding bits of two operands. The result bit is 1 only when both corresponding bits are 1. Otherwise, the result bit is 0.

Bitwise AND Operator दो Operands की संबंधित Bits की तुलना करता है। यदि दोनों Bits का मान 1 होता है, तभी परिणाम 1 प्राप्त होता है। अन्य सभी स्थितियों में परिणाम 0 होता है।

Truth Table (सत्यता तालिका)

ABA & B
000
010
100
111

Example (उदाहरण)

A = 12 = 1100
B = 5 = 0101
----
A & B = 0100 = 4

C Program:

#include <stdio.h>
int main(void)
{
int A = 12, B = 5;
printf("Bitwise AND = %d\n", A & B);
return 0;
}

Output:

Bitwise AND = 4

2. Bitwise OR Operator (|)

The Bitwise OR operator compares corresponding bits of two operands. The result bit is 1 if at least one of the corresponding bits is 1. The result is 0 only when both bits are 0.

Bitwise OR Operator दो Operands की संबंधित Bits की तुलना करता है। यदि किसी एक या दोनों Bits का मान 1 होता है, तो परिणाम 1 प्राप्त होता है। परिणाम 0 केवल तभी होता है जब दोनों Bits का मान 0 हो।

Truth Table (सत्यता तालिका)

ABA | B
000
011
101
111

Example (उदाहरण)

A = 12 = 1100
B = 5 = 0101
----
A | B = 1101 = 13

C Program:

#include <stdio.h>
int main(void)
{
int A = 12, B = 5;
printf("Bitwise OR = %d\n", A | B);
return 0;
}

Output:

Bitwise OR = 13

3. Bitwise XOR Operator (^)

The Bitwise XOR (Exclusive OR) operator compares corresponding bits of two operands. The result bit is 1 when the two bits are different and 0 when they are the same.

Bitwise XOR (Exclusive OR) Operator दो Operands की संबंधित Bits की तुलना करता है। यदि दोनों Bits अलग-अलग होती हैं, तो परिणाम 1 प्राप्त होता है और यदि दोनों Bits समान होती हैं, तो परिणाम 0 प्राप्त होता है।

Truth Table (सत्यता तालिका)

ABA ^ B
000
011
101
110

Example (उदाहरण)

A = 12 = 1100
B = 5 = 0101
----
A ^ B = 1001 = 9

C Program:

#include <stdio.h>
int main(void)
{
int A = 12, B = 5;
printf("Bitwise XOR = %d\n", A ^ B);
return 0;
}

Output:

Bitwise XOR = 9

4. Bitwise NOT Operator (~)

The Bitwise NOT operator is also known as the One's Complement operator. It is a unary operator that reverses every bit of its operand: 0 becomes 1 and 1 becomes 0.

Bitwise NOT Operator को One's Complement Operator भी कहा जाता है। यह एक Unary Operator है, जो अपने Operand की प्रत्येक Bit को उलट देता है। अर्थात 0 को 1 तथा 1 को 0 में बदल देता है।

Example (उदाहरण)

For a typical 32-bit signed integer representation:

A = 00000000 00000000 00000000 00001100
~A = 11111111 11111111 11111111 11110011

Therefore:

~12 = -13

For signed integers on systems using two's-complement representation, the result follows this rule:

~A = -(A + 1)
~12 = -(12 + 1) = -13

C Program:

#include <stdio.h>
int main(void)
{
int A = 12;
printf("Bitwise NOT = %d\n", ~A);
return 0;
}

Output (on typical two's-complement systems):

Bitwise NOT = -13

5. Left Shift Operator (<<)

The Left Shift operator shifts the bits of the left operand towards the left by the number of positions specified by the right operand. Vacated positions on the right are filled with zeros.

Left Shift Operator, Left Operand की Bits को Right Operand द्वारा बताई गई Positions के अनुसार बाईं ओर Shift करता है। दाईं ओर खाली होने वाली Positions में 0 भर दिया जाता है।

Example (उदाहरण)

A = 12 = 1100
A << 1 = 11000 = 24
A << 2 = 110000 = 48

C Program:

#include <stdio.h>
int main(void)
{
int A = 12;
printf("Left Shift by 1 = %d\n", A << 1);
printf("Left Shift by 2 = %d\n", A << 2);
return 0;
}

Output:

Left Shift by 1 = 24
Left Shift by 2 = 48

6. Right Shift Operator (>>)

The Right Shift operator shifts the bits of the left operand towards the right by the number of positions specified by the right operand.

Right Shift Operator, Left Operand की Bits को Right Operand द्वारा बताई गई Positions के अनुसार दाईं ओर Shift करता है।

Example (उदाहरण)

A = 12 = 1100
A >> 1 = 0110 = 6
A >> 2 = 0011 = 3

C Program:

#include <stdio.h>
int main(void)
{
int A = 12;
printf("Right Shift by 1 = %d\n", A >> 1);
printf("Right Shift by 2 = %d\n", A >> 2);
return 0;
}

Output:

Right Shift by 1 = 6
Right Shift by 2 = 3

Complete C Program Using All Bitwise Operators (सभी बिटवाइज ऑपरेटर्स का उपयोग करने वाला प्रोग्राम)

The following program demonstrates all six bitwise operators using A = 12 and B = 5.

निम्न प्रोग्राम में A = 12 तथा B = 5 का उपयोग करके सभी छह Bitwise Operators को प्रदर्शित किया गया है।

#include <stdio.h>
int main(void)
{
int A = 12, B = 5;
printf("A = %d, B = %d\n", A, B);
printf("Bitwise AND (A & B) = %d\n", A & B);
printf("Bitwise OR (A | B) = %d\n", A | B);
printf("Bitwise XOR (A ^ B) = %d\n", A ^ B);
printf("Bitwise NOT (~A) = %d\n", ~A);
printf("Left Shift (A << 2) = %d\n", A << 2);
printf("Right Shift (A >> 2)= %d\n", A >> 2);
return 0;
}

Output (आउटपुट):

A = 12, B = 5
Bitwise AND (A & B) = 4
Bitwise OR (A | B) = 13
Bitwise XOR (A ^ B) = 9
Bitwise NOT (~A) = -13
Left Shift (A << 2) = 48
Right Shift (A >> 2)= 3

Important Points About Bitwise Operators (बिटवाइज ऑपरेटर्स के महत्वपूर्ण बिंदु)

  1. Bitwise operators work on the bits of integer operands. They are not used directly with floating-point operands.

    बिटवाइज ऑपरेटर्स Integer Operands की Bits पर कार्य करते हैं। इनका उपयोग सीधे Floating-Point Operands के साथ नहीं किया जाता है।

  2. The &, | and ^ operators are binary operators because they require two operands.

    &, | तथा ^ Binary Operators हैं क्योंकि इन्हें दो Operands की आवश्यकता होती है।

  3. The ~ operator is a unary operator because it requires only one operand.

    ~ एक Unary Operator है क्योंकि इसे केवल एक Operand की आवश्यकता होती है।

  4. The << and >> operators are shift operators. The right operand specifies the number of bit positions to shift.

    << तथा >> Shift Operators हैं। इनका Right Operand बताता है कि कितनी Bit Positions को Shift करना है।

  5. Bitwise AND (&) and Logical AND (&&) are different operators. Similarly, Bitwise OR (|) and Logical OR (||) are different.

    Bitwise AND (&) तथा Logical AND (&&) अलग-अलग Operators हैं। इसी प्रकार Bitwise OR (|) तथा Logical OR (||) भी अलग-अलग हैं।

  6. Shift operations must use a non-negative shift count that is smaller than the width of the promoted left operand. Invalid shift counts can cause undefined behavior.

    Shift Operations में Shift Count शून्य या धनात्मक होना चाहिए तथा Promoted Left Operand की Bit Width से कम होना चाहिए। गलत Shift Count का उपयोग करने से Undefined Behavior हो सकता है।

Bitwise operators are important in C programming because they allow programmers to manipulate individual bits efficiently. The six commonly used bitwise operators are AND (&), OR (|), XOR (^), NOT (~), Left Shift (<<) and Right Shift (>>).

C Programming में Bitwise Operators महत्वपूर्ण हैं क्योंकि इनके द्वारा किसी Integer की अलग-अलग Bits को प्रभावी ढंग से नियंत्रित किया जा सकता है। इनके छह मुख्य प्रकार हैं – AND (&), OR (|), XOR (^), NOT (~), Left Shift (<<) तथा Right Shift (>>)।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025