IT

What do you need to know about Python bitwise operators? [Explained with Examples]

Computer systems represent, store, and send data as a stream of binary numbers called bits. “Binary” means that only two possible values, 0 and 1, can exist, and each such binary is called a “bit”. Whether you use text, decimal, image, audio, or video, computer software converts the information into a binary code consisting of 0s and 1s. Binary information, often referred to as machine language, is the most basic level of information stored by computer systems.

Python bitwise operators allow you to manipulate individual bits of data at the most basic level. That is, Python bitwise operators are used to perform bitwise operations on integer values. Therefore, integer values ​​are first converted to binary format, and then bit-by-bit operations are performed. After the “bitwise operation” is performed, the result is returned in decimal format. Python bitwise operators only work with integers.Improve yourself with Data science program Helps you overcome challenges. Let’s talk more about text mining.

Now let’s take a closer look at Python’s bitwise operators, types, their behavior, and examples.

Python bitwise operator

Python contains six operators for performing bitwise logical operations on integers.

Below is a description of the various bitwise operators in Python, their syntax, and their capabilities.

operator

syntax

Explanation

Bit-by-bit AND

a & b

If both bits of the operand are 1, the operator returns 1. Otherwise, it returns 0.

Bit-by-bit OR

a | b

If any bit of the operand is 1, the operator returns 1. Otherwise, it returns 0.

Not in bits

~ A

A unary bit operator that performs a logical negation of a specified number by inverting all bits. Arithmetically, it is expressed by subtracting the individual bit values ​​from 1 (~ ai = 1 – ai).

Bitwise XOR

a ^ b

If the bit pair contains the opposite bit value, the operator returns 1. Otherwise, it returns 0.

Bitwise left shift

a <

The operator shifts the bits of the first operand to the left by the number of digits defined in the second operand. In addition, the operator adds enough zeros to fill the gap formed to the right of the new bit pattern.

Bitwise shift to the right

a >>

The operator pushes the bits to the right by the specified number of digits. Therefore, the rightmost bit is always dropped.

Python bitwise operator with example

With a basic explanation of each bit operator in mind, To further clarify the concept, let’s look at some examples.

1. Bit-by-bit AND (&)

Returns 1 if both bits are 1. Otherwise, it returns 0.

a = 24 = 11000 (binary)

b = 4 = 00100 (binary)

a & b = 11000

&

00100

= 00000

= 0 (decimal number)

a = 10 = 1010 (binary)

b = 2 = 0010 (binary)

a & b = 1010

&

0010

= 0010

= 2 (decimal)

2. Bit-by-bit OR (|)

Returns 1 if any bit is 1, otherwise 0.

a = 67 = 1000011 (binary)

b = 54 = 0110110 (binary)

a | b = 1000011

|

0110110

= 1110111

= 119 (decimal)

a = 6 = 000110 (binary)

b = 34 = 100010 (binary)

a | b = 000110

|

100010

= 100110

= 38 (decimal)

3. Bitwise NOT (~)

Returns the one’s complement of the bit (inverts the bit).

a = 60 = 111100 (binary)

~ A = ~ 111100

= 000011

= 3 (decimal)

a = 33 = 100001 (binary)

~ A = ~ 100001

= 011110

= 30 (decimal)

4. Bitwise XOR (^)

Returns 1 if the values ​​of both bits are opposite. Otherwise, it returns 0.

a = 61 = 0111101 (binary)

b = 90 = 1011010 (binary)

a ^ b = 0111101

^ ^

1011010

= 1100111

= 103 (decimal)

a = 2 = 000010 (binary)

b = 55 = 110111 (binary)

a ^ b = 000010

^ ^

110111

= 110101

= 53 (decimal)

5. Bitwise left shift (<<)

Shifts the bit to the left and fills the gap formed on the right with zeros.

a = 39 = 100111 (binary)

a << 1 = 1001110 = 78 (decimal)

a << 2 = 10011100 = 156 (decimal)

a << 3 = 100111000 = 312 (decimal)

b = 74 = 1001010 (binary)

b << 1 = 10010100 = 148 (decimal)

b << 2 = 100101000 = 296 (decimal number)

b << 3 = 1001010000 = 592 (decimal)

6. Bitwise right shit (>>)

Shifts the bit to the right and fills the gap formed on the left with zeros.

a = 157 = 10011101 (binary)

a >> 1 = 1001110 = 78 (decimal)

a >> 2 = 100111 = 39 (decimal)

a >> 3 = 10011 = 19 (decimal)

b = 89 = 1011001 (binary)

b >> 1 = 101100 = 44 (decimal)

b >> 2 = 010110 = 22 (decimal)

b >> 3 = 001011 = 11 (decimal)

Python bit operators (example using code)

Below is a simple code snippet to explain Python’s bitwise operators.

1. Bitwise AND, OR, NOT, and XOR

Source

Source

2. Bitwise shift operator

Source

Source

Future Direction: Acquire Python skills in upGrad’s Professional Certification Program in Data Science

Are you an ambitious business manager with little coding experience?Then upGrad will give you A data science professional qualification program for business decision making.

The Data Science Professional Certification Program is specially designed for managers and working professionals who want to develop basic data science skills and hands-on knowledge. The rigorous eight-month online program focuses on data science issues, algorithms, and how to present data to make important business decisions. The curriculum covers the following courses:

  • Pre-program preparation content
  • Business problem solving, insights, storytelling
  • Statistics and machine learning
  • strategy
  • Capstone project
  • Python overview (optional module)

The main highlights of the program are:

  • Globally recognized certification from IIM Calicut
  • Over 200 hours of content, over 20 live learning sessions, over 5 expert coaching sessions
  • Three industry projects and climax
  • Comprehensive coverage of Excel, Python, Tableau, R, and Power BI
  • 360 degree career support
  • One-on-one with industry mentors
  • Employment support with top companies
  • Industry networking opportunities

Conclusion

Python is a powerful, versatile, and easy-to-use programming language that is popular with both professionals and beginners. What’s more, Python is still the programming language of choice among developers due to its pure efficiency, versatility, reliability, and speed. It can be used in almost all types of environments and platforms without significantly degrading performance. In addition, Python enhances most of the data processing workloads across many organizations. The ease of data analysis provided by Python is useful for many applications, including machine learning projects.

Python forms a fundamental aspect of data science and is undoubtedly useful for tackling critical business processes. Moreover, as big data grows and more and more companies focus on data-driven decision making, Python’s importance cannot be undermined. So if you want to learn Python and apply your skills to data-driven business scenarios, Sign up for upGrad To learn from the best in the industry!

What are bitwise operators used for?

Bitwise operations in Python are used to perform bitwise operations on integer values. For this reason, integers are first converted to binary, then various operations are performed bit by bit, and the result is returned in decimal format. In Python, bitwise operators can only be used with integers.

Is the bit operator fast?

On simple, low-cost processors, bitwise operators are usually faster than division, multiplication, and often significantly faster than addition. In general, bitwise operators use less resources and therefore use significantly less power.

What is the use of logical operators in Python?

Python has three logical operators that allow you to compare values. The three logical operators AND, OR, and NOT evaluate the expression to a Boolean value and return True or False based on the result of the operator. Here are three Python logical operators:

1. AND (a and b): true if both expressions are true
2. OR (a or b): True if at least one expression is true
3. NOT (not a): true only if the expression is false

Prepare for future careers

https://www.upgrad.com/blog/python-bitwise-operators/ What do you need to know about Python bitwise operators? [Explained with Examples]

Back to top button