Bitwise Operators कैसे काम करते हैं
Bitwise operators किसी number के अंदर के छोटे on-off switches पर काम करते हैं, जैसे एक लंबी पंक्ति में अलग-अलग light switches flip करना। ये settings को compactly साथ पैक करने के लिए उपयोगी हैं।
In this page:
Syntax
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
&&का मतलब होने पर&का उपयोग करना, और bool की जगह numbers पर एक bitwise result पाना।- type size से ज़्यादा bits से shift करना, जो shift count को wrap कर देता है।
- यह भूल जाना कि
~हर bit flip कर देता है, इसलिएintके लिए~5-6 है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: