← Back to C# Course | Chapter 3: Operators | Lesson 4 of 6

Bitwise Operators कैसे काम करते हैं

Bitwise operators किसी number के अंदर के छोटे on-off switches पर काम करते हैं, जैसे एक लंबी पंक्ति में अलग-अलग light switches flip करना। ये settings को compactly साथ पैक करने के लिए उपयोगी हैं।

In this page:

  1. Bitwise Operators
Syntax
csharp
int result = a & b;    // AND
int result = a | b;    // OR
int result = a ^ b;    // XOR
int result = ~a;       // NOT
int result = a << n;   // shift left
int result = a >> n;   // shift right

Bitwise Operators

Bitwise operators (&, |, ^, ~, <<, >>) integers के binary representation पर सीधे operate करते हैं। & और | हर bit पर AND/OR करते हैं, ^ XOR है, <</>> bits को बाएं या दाएं shift करते हैं।

ये flags, masks, और low-level performance code में आम हैं।

Note: x << 1 किसी integer को double कर देता है, और x >> 1 उसे half कर देता है (non-negative numbers के लिए)।

उदाहरण: Bitwise Operators

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        int a = 6;  // 110
        int b = 3;  // 011

        Console.WriteLine(a & b);   // 010 = 2
        Console.WriteLine(a | b);   // 111 = 7
        Console.WriteLine(a ^ b);   // 101 = 5
        Console.WriteLine(a << 1);  // 1100 = 12
        Console.WriteLine(a >> 1);  // 011 = 3
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. && का मतलब होने पर & का उपयोग करना, और bool की जगह numbers पर एक bitwise result पाना।
  2. type size से ज़्यादा bits से shift करना, जो shift count को wrap कर देता है।
  3. यह भूल जाना कि ~ हर bit flip कर देता है, इसलिए int के लिए ~5 -6 है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.