Python Bitwise Operators || bitwise operators in python, bitwise operators in python in hindi

Home 👉 O level 👉 Python 👉 Unit 4


 Bitwise Operators in Python

ये ऑपरेटर्स बिट्स पर ऑपरेशन करते हैं। मानों को पहले बाइनरी में बदला जाता है फिर कैलकुलेशन किया जाता है। ये ऑपरेटर्स केवल इन्टिजर मानों पर कार्य करते हैं।

These operators operate on bits. The values are first converted to binary then the calculations are done. These operators work only on integer values.

  • Bitwise AND ( & )
  • Bitwise OR ( | )
  • Bitwise XOR ( ^ )
  • Bitwise NOT ( ~ )
  • Bitwise Left Shift ( << )
  • Bitwise Right Shift ( >> )



Bitwise AND (&):-

    यह बाइनरी संख्याओं के बीच AND ऑपरेशन करता है। यदि दोनों बिट संख्याएं 1 है तो परिणाम 1 मिलता है अन्यथा 0 मिलता है। पायथन में, बिटवाइज़ AND ऑपरेटर को एम्परसेंड प्रतीक `&` द्वारा दर्शाया जाता है। इसका उपयोग दो पूर्णांकों के अलग-अलग बिट्स पर बिटवाइज AND ऑपरेशन करने के लिए किया जाता है। परिणाम में प्रत्येक बिट स्थिति में 1 होगा जहां दोनों ऑपरेंड में 1 होगा। इस ऑपरेटर का उपयोग अक्सर निम्न-स्तरीय बिट हेरफेर और बाइनरी में कुछ प्रकार के flags या अनुमतियों की जांच के लिए किया जाता है।

    It performs AND operation between binary numbers. If both the bit numbers are 1 then the result is 1 otherwise 0 is returned. In Python, the bitwise AND operator is represented by the ampersand symbol `&`. It is used to perform a bitwise AND operation on the individual bits of two integers. The result will have a 1 in each bit position where both operands have a 1. This operator is often used for low-level bit manipulation and certain types of flags or permissions checking in binary.


syntax: x & y
example: 5 & 6
result: 4

Hint:     

Bitwise OR (|):-

    यह बाइनरी संख्याओं के बीच OR ऑपरेशन करता है। यदि दोनों बिट संख्याएं 0 है तो परिणाम 0 मिलता है अन्यथा 1 मिलता है। पायथन में, बिटवाइज़ OR ऑपरेटर को `|` प्रतीक द्वारा दर्शाया जाता है। यह दो पूर्णांकों के संगत बिट्स की प्रत्येक जोड़ी पर बिटवाइज OR ऑपरेशन करता है। परिणाम में किसी भी बिट स्थिति में 1 होगा जहां कम से कम एक ऑपरेंड में 1 होगा।

    It performs OR operation between binary numbers. If both the bit numbers are 0 then the result is 0 otherwise 1 is returned. In Python, the bitwise OR operator is represented by the `|` symbol. It performs a bitwise OR operation on each pair of corresponding bits of two integers. The result will have a 1 in any bit position where at least one of the operands has a 1.


syntax: x | y
example: 5 | 6
result: 7

Hint:


Bitwise XOR (Exclusive OR) (^):-

यह बाइनरी संख्याओं के बीच XOR ऑपरेशन करता है। यदि दोनों बिट संख्याएं समान हैं तो परिणाम 0 मिलता है अन्यथा 1 मिलता है। पायथन में, बिटवाइज़ XOR ऑपरेटर को कैरेट प्रतीक (^) द्वारा दर्शाया जाता है। इसका उपयोग दो पूर्णांकों के संगत बिट्स के बीच बिटवाइज़ एक्सक्लूसिव OR संचालन करने के लिए किया जाता है।


बिटवाइज़ XOR ऑपरेटर के लिए सामान्य उपयोग के मामलों में शामिल हैं:

  • फ़्लिपिंग बिट्स: आप किसी पूर्णांक में विशिष्ट बिट्स को टॉगल करने के लिए XOR का उपयोग कर सकते हैं, जो बिटफ़ील्ड में विकल्पों या फ़्लैग्स को टॉगल करने जैसे कार्यों में उपयोगी हो सकता है।
  • एन्क्रिप्शन: डेटा को स्क्रैम्बल करने के लिए कुछ सरल एन्क्रिप्शन एल्गोरिदम में XOR का उपयोग किया जाता है।
  • त्रुटि का पता लगाना और सुधार: XOR का उपयोग डेटा ट्रांसमिशन में त्रुटियों का पता लगाने और उन्हें ठीक करने के लिए त्रुटि-जांच एल्गोरिदम में किया जा सकता है।
  • रैंडम नंबर जेनरेशन: एक्सओआर ऑपरेशन का उपयोग रैंडम-जैसे अनुक्रम उत्पन्न करने के लिए छद्म-यादृच्छिक संख्या जनरेटर (पीआरएनजी) में किया जा सकता है।

याद रखें कि बिटवाइज़ ऑपरेशन निम्न स्तर के होते हैं और बिट स्तर पर काम करते हैं, इसलिए इन्हें अन्य अंकगणितीय या तार्किक ऑपरेशनों की तरह आमतौर पर उपयोग नहीं किया जा सकता है।

    It performs XOR operation between binary numbers. If both the bit numbers are same then the result is 0 otherwise 1 is returned. In Python, the bitwise XOR operator is represented by the caret symbol (^). It is used to perform bitwise exclusive OR operations between corresponding bits of two integers.

Common use cases for the bitwise XOR operator include:

  • Flipping bits: You can use XOR to toggle specific bits in an integer, which can be useful in tasks like toggling options or flags in a bitfield.
  • Encryption: XOR is used in some simple encryption algorithms to scramble data.
  • Error detection and correction: XOR can be used in error-checking algorithms to detect and correct errors in data transmission.
  • Random number generation: XOR operations can be be used in pseudo-random number generators (PRNGs) to generate random-like sequences.

Remember that bitwise operations are low-level and work at the bit level, so they may not be as commonly used as other arithmetic or logical operations.

syntax: x ^ y
example: 5 ^ 6
result: 3

Hint:


Bitwise NOT (~):-

    यह एक बिटवाइज़ ऑपरेटर होता है जो एक संख्या के सभी बिट्स को उलट देता है। जब एक संख्या के सभी बिट्स को उलटा दिया जाता है, तो सभी 0 बिट्स 1 बन जाते हैं और सभी 1 बिट्स 0 बन जाते हैं। पायथन में, बिटवाइज़ NOT ऑपरेटर को टिल्ड (~)  द्वारा दर्शाया जाता है। इसका उपयोग किसी पूर्णांक के अलग-अलग बिट्स पर बिटवाइज़ नेगेशन करने के लिए किया जाता है। जब आप किसी बाइनरी नंबर पर बिटवाइज नॉट ऑपरेटर लागू करते हैं, तो यह प्रत्येक बिट को उलट देता है, 0s को 1s में और 1s को 0s में बदल देता है।

बिटवाइज़ NOT ऑपरेटर के लिए उपयोग के मामलों में शामिल हैं:

  • मास्क और फ़िल्टर: बाइनरी डेटा या फ़्लैग के साथ काम करते समय, आप पूर्णांक के भीतर विशिष्ट बिट्स को निकालने या हेरफेर करने के लिए मास्क या फ़िल्टर बनाने के लिए बिटवाइज़ NOT ऑपरेटर का उपयोग कर सकते हैं।
  • एन्कोडिंग और डिकोडिंग: कुछ मामलों में, कस्टम एन्कोडिंग और डिकोडिंग योजनाओं में बिटवाइज़ NOT का उपयोग किया जा सकता है।

    यह ध्यान रखना महत्वपूर्ण है कि आप जिस समस्या या संदर्भ में काम कर रहे हैं, उसके आधार पर विशिष्ट उपयोग के मामले भिन्न हो सकते हैं। बिटवाइज़ ऑपरेटरों का उपयोग आमतौर पर निम्न-स्तरीय प्रोग्रामिंग, हार्डवेयर इंटरैक्शन और उन स्थितियों में किया जाता है जहां आपको डेटा के भीतर अलग-अलग बिट्स में हेरफेर करने की आवश्यकता होती है।

It is a bitwise operator that reverses all the bits of a number. When all bits of a number are inverted, all 0 bits become 1 and all 1 bits become 0. In Python, the bitwise NOT operator is represented by the tilde (~) character. It is used to perform bitwise negation on individual bits of an integer. When you apply the bitwise NOT operator to a binary number, it inverts each bit, turning 0s into 1s and 1s into 0s.

Use cases for the bitwise NOT operator include:

  • Masks and Filters: When working with binary data or flags, you can use the bitwise NOT operator to create masks or filters to extract or manipulate specific bits within an integer.
  • Encoding and Decoding: In some cases, bitwise NOT can be used in custom encoding and decoding schemes.

It's important to note that the specific use cases may vary depending on the problem or context in which you are working. Bitwise operators are most commonly used in low-level programming, hardware interaction, and situations where you need to manipulate individual bits within data.

syntax: ~x
example: ~5
result: -6

Hint:

(result is calculated on 8-bit machine)



Bitwise Left Shift (<<):-

यह किसी संख्या के बिट्स को दिए गए अंकों तक बायीं ओर खिसकाता है। पायथन में, बिटवाइज़ लेफ्ट शिफ्ट ऑपरेटर (`<<`) का उपयोग बाइनरी नंबर के बिट्स को निर्दिष्ट संख्या में स्थिति द्वारा बाईं ओर शिफ्ट करने के लिए किया जाता है।

बिटवाइज़ लेफ्ट शिफ्ट ऑपरेटर के लिए सामान्य उपयोग के मामलों में शामिल हैं:

  • प्रदर्शन अनुकूलन: `*` और `/` ऑपरेटरों का उपयोग करने के बजाय बिटवाइज़ लेफ्ट शिफ्ट और राइट शिफ्ट का उपयोग करके 2 की शक्तियों से गुणा या भाग करना अधिक कुशल हो सकता है।
  • 2 की शक्तियाँ बनाना: विभिन्न उद्देश्यों के लिए 2 की शक्तियाँ त्वरित रूप से उत्पन्न करना, जैसे मेमोरी आवंटन रणनीतियाँ स्थापित करना या हार्डवेयर रजिस्टरों के साथ काम करना।
  • बाइनरी प्रतिनिधित्व: संख्यात्मक संचालन के लिए दशमलव और बाइनरी प्रतिनिधित्व के बीच रूपांतरण।

यह ध्यान रखना महत्वपूर्ण है कि बिटवाइज़ ऑपरेटरों के उपयोग में अक्सर निम्न-स्तरीय ऑपरेशन शामिल होते हैं और यह आमतौर पर उन स्थितियों में पाया जाता है जहां आपको डेटा के व्यक्तिगत बिट्स में हेरफेर करने की आवश्यकता होती है। यह ऐसी चीज़ नहीं है जिसे आप आमतौर पर रोजमर्रा की प्रोग्रामिंग में उपयोग करते हैं, लेकिन सिस्टम-स्तरीय प्रोग्रामिंग या एम्बेडेड सिस्टम जैसे कुछ विशेष अनुप्रयोगों में यह आवश्यक है।

It shifts the bits of a number to the left up to the given digit. In Python, the bitwise left shift operator (`<<`) is used to shift the bits of a binary number to the left by a specified number of positions. 

Common use cases for the bitwise left shift operator include:

  • Performance Optimization: It can be more efficient to multiply or divide by powers of 2 using bitwise left shift and right shift instead of using the `*` and `/` operators.
  • Creating Powers of 2: Quickly generating powers of 2 for various purposes, such as setting up memory allocation strategies or working with hardware registers.
  • Binary Representation: Converting between decimal and binary representations for numerical operations.

It's important to note that the use of bitwise operators often involves low-level operations and is typically found in situations where you need to manipulate the individual bits of data. It's not something you'd commonly use in everyday programming but is essential in certain specialized applications, like system-level programming or embedded systems.

syntax: value  << bits
example: 5 << 2
result: 20

Hint:


Bitwise Right Shift (>>):-

यह किसी संख्या के बिट्स को दिए गए अंकों तक दायीं ओर खिसकाता है। पायथन में, बिटवाइज़ राइट शिफ्ट ऑपरेटर को `>>` द्वारा दर्शाया जाता है। इसका उपयोग किसी बाइनरी संख्या के बिट्स को एक निश्चित संख्या में स्थिति द्वारा दाईं ओर स्थानांतरित करने के लिए किया जाता है। ऑपरेटर दो ऑपरेंड लेता है: बायां ऑपरेंड स्थानांतरित की जाने वाली संख्या है, और दायां ऑपरेंड बिट्स को स्थानांतरित करने की स्थिति की संख्या है।

बिटवाइज़ राइट शिफ्ट ऑपरेटरों का उपयोग अक्सर निम्न-स्तरीय प्रोग्रामिंग और अनुकूलन उद्देश्यों के लिए किया जाता है, जैसे कि बाइनरी डेटा के साथ काम करते समय या अंकगणितीय संचालन करते समय जिसे बिटवाइज़ हेरफेर का उपयोग करके अनुकूलित किया जा सकता है। 

कुछ सामान्य उपयोग के मामलों में शामिल हैं:

  • 2 की घात से विभाजित करना: एक निश्चित संख्या के स्थान से दाएँ स्थानांतरण उस संख्या की घात को 2 से विभाजित करने के बराबर है। यह डिवीजन ऑपरेटर (`/`) का उपयोग करने से अधिक कुशल हो सकता है।

  • मेमोरी और स्टोरेज को अनुकूलित करना: बिटवाइज़ ऑपरेशंस का उपयोग कभी-कभी मेमोरी या स्टोरेज स्पेस को बचाने के लिए किया जाता है, विशेष रूप से एम्बेडेड सिस्टम में या बड़े डेटासेट के साथ काम करते समय।
  • कुशल डेटा प्रोसेसिंग: कुछ एल्गोरिदम और डेटा हेरफेर कार्यों में, प्रदर्शन को अनुकूलित करने के लिए बिटवाइज़ संचालन का उपयोग किया जा सकता है।

जबकि बिटवाइज़ ऑपरेशन शक्तिशाली और कुशल हो सकते हैं, उनका उपयोग सावधानी से किया जाना चाहिए क्योंकि वे कोड को कम पठनीय और अधिक त्रुटि-प्रवण बना सकते हैं। इसलिए, वे आम तौर पर उन परिदृश्यों में नियोजित होते हैं जहां प्रदर्शन अनुकूलन महत्वपूर्ण होता है, जैसे निम्न-स्तरीय प्रोग्रामिंग में या हार्डवेयर-स्तर के संचालन के साथ काम करते समय।

It shifts the bits of a number to the right up to a given digit. In Python, the bitwise right shift operator is represented by `>>`. It is used to shift the bits of a binary number to the right by a specified number of positions. The operator takes two operands: the left operand is the number to be shifted, and the right operand is the number of positions to shift the bits.

Bitwise right shift operators are often used in low-level programming and for optimization purposes, such as when working with binary data or performing arithmetic operations that can be optimized using bitwise manipulation. 

Some common use cases include:

  • Dividing by Powers of 2: Right shifting by a certain number of positions is equivalent to dividing by 2 to the power of that number. This can be more efficient than using the division operator (`/`).
  • Optimizing Memory and Storage: Bitwise operations are sometimes used to save memory or storage space, especially in embedded systems or when dealing with large datasets.
  • Efficient Data Processing: In some algorithms and data manipulation tasks, bitwise operations can be used to optimize performance.

While bitwise operations can be powerful and efficient, they should be used with caution because they can make code less readable and more error-prone. Therefore, they are typically employed in scenarios where performance optimization is critical, such as in low-level programming or when working with hardware-level operations.


syntax: value >> bits
example: 5 >> 2
result: 1

Hint:


NEXT:-

Comments