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