Min menu

Pages

Operators that are used to deal with bits (Bitwise Operators) in Python

Factors that are used to deal with bits (Bitwise Operators) in Python 

the name of the employeehis codeExampleExplanation of the code
Bitwise AND&a & bThe operator  & calculates the product of the sum of bits common between  a and b
Bitwise OR|a | bThe operator  | calculates the product of the sum of bits Common and unshared between  a and b
Bitwise XOR^a ^ bThe operator  ^ calculates the product of the sum of bits unshared between  a and b
Bitwise compliment OR~~aThe worker  ~ turns the bits which equals  0  to  1  and inverts the bits Which equals  1  to  0 ,
then add  1 to them  and calculate their sum, then multiply the answer by the factor - ) Then he gives us a negative answer.
left shift<<a << 2The factor  << displaces the bits From the last left to the first right.
The number  2  means that we will remove the last two bits And we put them first.
Right shift>>a >> 2The factor  >> displaces the bits From the first right to the last left.
The number  2  means that we will remove the first two bits And we put them last.

Here we put an example for each factor in the table

Examples of factors that are used to deal with Bits in python

Here is a set of examples for each of the factors that are used to deal with the Bits in Python.

worker  & (Bitwize AND)

The operator  & calculates the product of the sum of bits commonality between two values.

Example

Test.py
        a = 10 # a = 10 = 00000000000000000000000000001010
		  b = 75 # 7b = 75 = 00000000000000000000000001001011
	  
		  c = a & b # We explained how the output will be obtained under the run result
	  
		  print(a, '&', b, '=', c)
	

We will get the following result when running.

10 & 75 = 10

 Here we have taught the bits Common and collected in yellow.

a = 10;   // 10 = 0000000000000000000000000000 1 0 1 0
b = 75;   // 75 = 0000000000000000000000000100 1 0 1 1

c = a & b;   //  c = 0000000000000000000000000000 1 0 1 0
  //  c = 10


worker  | (Bitwize OR)

The operator  | calculates the product of the sum of bits The common and the unshared between two values.

Example

Test.py
        a = 10 # 10 = 00000000000000000000000001010
		  b = 75 #75 = 00000000000000000000000001001011
	  
		  c = a | b# We explained how the output will be obtained under the run result
	  
		  print(a, '|', b, '=', c)
	

We will get the following result when running.

10 | 75 = 75

 Here we have taught the bits Common and unshared and collected in yellow.

a = 10;   // 10 = 0000000000000000000000000000 1 0 1 0
b = 75;   // 75 = 0000000000000000000000000 1 00 1 0 11

c = a | b;   //  c = 0000000000000000000000000 1 00 1 0 11
  //  c = 75


worker  ^ (Bitwize XOR)

The operator  ^ calculates the product of the sum of bits Not shared by two values.

Example

Test.py
        a = 10 # 10 = 00000000000000000000000001010
		  b = 75 #75 = 00000000000000000000000001001011
	  
		  c = a ^ b # We explained how the output will be obtained under the run result
	  
		  print(a, '^', b, '=', c)
	

We will get the following result when running.

10 ^ 75 = 65

 Here we have taught the bits Unshared and collected in yellow.

a = 10;   // 10 = 00000000000000000000000000001010
b = 75;   // 75 = 0000000000000000000000000 1 00101 1

c = a | b;   //  c = 0000000000000000000000000 1 00000 1
  //  c = 65


worker  ~ (Bitwize Compliment OR)

The worker  ~ turns the bits which equals  0  to  1  and inverts the bits which equals  1  to  0 .
Then the output is calculated according to the principle single precision floating point number.

Example

Test.py
        a = 10 # 10 = 00000000000000000000000001010
		  c = ~a # c = 11111111111111111111111111111110111 = -11
	  
		  print('~', a, '=', c)
	

We will get the following result when running.

~10 = -11


worker  << (Left Shift)

worker  << wipes bits from the left side and then switch all bit It was erased from them with a zero and puts them on the right.

Example

Test.py
        a = 10 # 10 = 00000000000000000000000001010
		  c = a << 2 # We explained how the output will be obtained under the operation result
	  
		  print(a, '<< 2 =', c)
	

We will get the following result when running.

10 << 2 = 40

 Here we have taught the bits Which was erased in yellow and marked with bits which have been added in blue.

a = 10;   // 10 =  00 000000000000000000000000001010

c = a << 2;   //  c = 000000000000000000000000001010 00  = 40
  //  c = 40


worker  >> (Right Shift)

The factor  >> has two states: the number may be greater than zero or less than zero.

  • If the number is greater than zero, erase bits on the right, then switch each bit them with zero and puts them on the left.

  • If the number is less than zero, erase bits on the right, then switch each bit One of them to keep the minus sign and put them to the left.


first example

First case: If the number is greater than zero.

Test.py
        a = 9 #9 = 00000000000000000000000000001001
		  c = a >> 2 # We explained how the output will be obtained under the operation result
	  
		  print(a, '>> 2 =', c)
	

We will get the following result when running.

9 >> 2 = 2

 Here we have taught the bits Which was erased in yellow and marked with bits which have been added in blue.

a = 9;   // 9 = 000000000000000000000000000010 01

c = a >> 2;   // c =  00 000000000000000000000000000010
  // c = 2

second example

The second case: If the number is less than zero.

Test.py
        a = -9 # -9 = 111111111111111111111111111111000
		  c = a >> 2 # We explained how the output will be obtained under the operation result
	  
		  print(a, '>> 2 =', c)
	

We will get the following result when running.

-9 >> 2 = -3

 Here we have taught the bits Which was erased in yellow and marked with bits which have been added in blue.

a = -9;   // -9 = 111111111111111111111111111101 11

c = a >> 2;   //  c =  11 111111111111111111111111111101
  //  c = -3