← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 12 of 12

JS Bitwise Operators

Picture a row of light switches on a wall, each one either on or off; bitwise operators let you flip, combine, and compare those switches directly, one bit at a time, instead of working with the number as a whole. They're used far less often than everyday arithmetic operators, but they're essential for tasks like packing multiple true/false flags into a single number efficiently.
Syntax
javascript
a & b
a | b
a ^ b
~a
a << n
a >> n
a >>> n

AND, OR, XOR

& (AND) operator नतीजे के bit को 1 तभी करता है जब दोनों operand bits 1 हों, | (OR) उसे 1 करता है जब कोई भी bit 1 हो, और ^ (XOR) उसे 1 केवल तब करता है जब bits अलग हों।

JavaScript इन operators को लगाने से पहले दोनों operands को 32-bit integers में बदलती है, फिर नतीजे को वापस सामान्य number में बदल देती है।

उदाहरण: AND, OR, XOR

javascript
console.log(5 & 3);  // 1 - AND
console.log(5 | 3);  // 7 - OR
console.log(5 ^ 3);  // 6 - XOR

NOT और Two's Complement

~ (NOT) operator अपने operand के हर bit को पलट देता है, और क्योंकि JavaScript ऋणात्मक संख्याओं को two's complement से दर्शाती है, ~n हमेशा -(n + 1) के बराबर होता है।

XOR (^) को अक्सर NOT के साथ विशिष्ट bits को दूसरों को प्रभावित किए बिना चालू या बंद करने के लिए इस्तेमाल किया जाता है।

उदाहरण: NOT and Two's Complement

javascript
console.log(~5); // -6, two's complement: -(5 + 1)

Left Shift और Right Shift

<< (left shift) operator bits को बाईं ओर खिसकाता है और शून्य से भरता है, जिसका असर दो की घात से गुणा करने जैसा होता है, जबकि >> (right shift) bits को दाईं ओर खिसकाता है, दो की घात से भाग देता है और ऋणात्मक संख्याओं का चिह्न सुरक्षित रखता है।

दोनों दो की घातों से गुणा या भाग के तेज़ विकल्प हैं।

उदाहरण: Left Shift and Right Shift

javascript
console.log(4 << 1); // 8 - multiply by 2
console.log(4 >> 1); // 2 - divide by 2
console.log(-8 >> 1); // -4, sign preserved

Unsigned Right Shift

>>> (unsigned right shift) operator भी bits को दाईं ओर खिसकाता है, लेकिन खाली हुए bits को चिह्न की परवाह किए बिना हमेशा शून्य से भरता है, यानी यह ऋणात्मक संख्याओं को उनका चिह्न सुरक्षित रखने की बजाय बड़ी धनात्मक संख्याओं की तरह मानता है।

यह इसे विशेष रूप से ऋणात्मक operands पर >> से अलग व्यवहार करने वाला बनाता है।

उदाहरण: Unsigned Right Shift

javascript
console.log(-1 >> 1);  // -1, sign preserved
console.log(-1 >>> 1); // large positive number, sign not preserved

व्यावहारिक Bit Flags

Bitwise operators का एक आम वास्तविक उपयोग कई true/false settings को एक अकेली संख्या में पैक करना है, जहाँ हर bit एक flag दर्शाता है, flags को जोड़ने के लिए OR और किसी विशिष्ट flag के set होने की जाँच के लिए AND का उपयोग करके।

यह पैटर्न permission systems, game state और निचले स्तर के protocol handling में दिखता है जहाँ memory की दक्षता मायने रखती है।

उदाहरण: Practical Bit Flags

javascript
const READ = 1;    // 0b001
const WRITE = 2;   // 0b010
const EXECUTE = 4; // 0b100
let permissions = READ | WRITE; // combine flags
console.log((permissions & WRITE) !== 0); // check if WRITE is set
Live Example
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. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 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.