Python Bitwise OperatorsIntroduction:In this article, we are discussing bitwise operators in Python. In Python, there are two kinds of operators: Logical operator and Bitwise operator. Here we mainly discuss Bitwise operators. Python operators are often used to handle values and arguments. This tutorial will explore a specific Python operator called the bitwise operator. These symbols indicate conventional symbols for mathematical and logical procedures. Bitwise OperatorsBitwise operations are processes that engage with individual bits, which are the fundamental constituent of any sort of data in a computer. A binary value of 0 or 1 is assigned to each bit. Regardless of the notion that machines can manipulate bits, machines usually store data and perform commands in bytes, which are bit multiples. Most scripting languages work with groups of 8 bits, 16 bits, or 32 bits. Bitwise operators are characters that denote operations that are performed on single bits. A bitwise operation is executed by spatially aligning the distinct bits of two-bit patterns of the same size. Bitwise Operators in PythonBitwise operators are employed in python to perform bitwise operations on numbers. The values are first converted to binary, and then manipulations are done bit by bit, hence the phrase "bitwise operators." The outcome is then displayed in decimal numbers. Bitwise Logical Operator: These operators are employed to execute logical operations like other logical operators bit by bit. This is akin to using logical operators such as and, or, and not on a bit level. Except for these basic facts, bitwise and logical operators are analogous. Bitwise Shift Operators: These operators multiply or divide an integer number by two by shifting every individual bit to the left or right. We can use them when we wish to multiply or divide a value by a power of 2. Python's bitwise operators only work with integers.
Every binary bitwise operator has a compound operator that performs an enhanced application.
These are rules for updating the left-hand argument whilst it's still active. Bitwise ANDThe logical conjunction is performed by the bitwise AND operation (&) on the appropriate bits of the provided operands. Program Code 1: Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - x & y = 0 Program Code 2: Here we give an example of bitwise AND in Python by taking user input into it. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - Enter the value of x: 5 Enter the value of y: 8 x & y = 0 Bitwise ORLogical disjunction is achieved using the bitwise OR operation (|). If, at minimum, one of the appropriate pair of bits is turned on, it yields a one. Program Code: Here we give an example of bitwise OR in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - x | y = 19 Bitwise NOTBecause the bitwise NOT operator only requires one parameter, this is the only unary bitwise operator. It flips all of the bits of a number given to implement logical negation upon it. It is denoted as ~x, where ~ symbol means negation. Program Code: Here we give an example of bitwise NOT in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - ~x = -44 Bitwise XORWhen one of the bits is 1, another is 0, it returns true; otherwise, it returns false. Program Code: Here we give an example of bitwise XOR in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - x ^ y = 10 Bitwise Right ShiftAs a result of this operation, the individual bits of the number are shifted to the right, and the gaps on the left are filled with 0 (or 1 if the number is negative). The result is similar to dividing a value by a power of 2. Program Code: Here we give an example of bitwise Right shift in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - x >> 1 = 13 y >> 1 = -13 Bitwise Left ShiftAs a result of the operation, the individual bits of the integer to the left are filled with 0 on the voids to the right. The result is similar to multiplying a value by a power of 2. Program Code: Here we give an example of bitwise Left shift in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - x << 1 = 104 y << 1 = -30 All operators in one code: In this code, we use all the bitwise operators. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - Enter the value of x: 4 Enter the value of y: 5 x & y = 4 x | y = 5 ~y = -6 x ^ y = 1 x >> 1 = 0 y >> 1 = 0 x << 1 = 8 y << 1 = 10 Next TopicPython Time asctime() Method |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India