Min menu

Pages

VB

Bitwise Operators in C++

What factors are used to deal with bits (Bitwise Operators)

the name of the employeehis codeExampleExplanation of the code
Bitwise AND&a & bThe operator  & calculates the sum of the bits common between  a and b
Bitwise OR|a | bThe operator  | calculates the sum of the bits Common and unshared between  a and b
Bitwise XOR^a ^ bThe operator  ^ calculates the sum of the bits unshared between  a and b
Bitwise compliment OR~~aThe worker  ~ turns the bits which equals  0  to  1  and stir the bits which equals  1  to 0 ,
Then he adds  1 to them  and calculates their sum, then multiplies the answer by the factor - ) Then he gives us a negative answer.
left shift<<a << 2worker  << 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 >> 2worker  >> 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.


Explain the factors that are used to deal with Bits in C++

worker  & (Bitwize AND) in C++

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

Example calculating the sum of bits shared between two values.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10; // a = 10 = 00000000000000000000000000001010
		  int b = 75; // b = 75 = 00000000000000000000000001001011
	  
		  int c = a & b; // We explained how the output will be obtained under the run result
	  
		  cout << a << " & " << b << " = " << c;
	  
		  return 0;
		  }
	

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) in C++

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

Example calculating the sum of bits The common and the unshared between two values.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10; // 10 = 00000000000000000000000000001010
		  int b = 75; // 75 = 00000000000000000000000001001011
	  
		  int c = a | b; // We explained how the output will be obtained under the run result
	  
		  cout << a << " | " << b << " = " << c;
	  
		  return 0;
		  }
	

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) in C++

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

Example calculating the sum of bits Not shared by two values.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10; // 10 = 00000000000000000000000000001010
		  int b = 75; // 75 = 00000000000000000000000001001011
	  
		  int c = a ^ b; // We explained how the output will be obtained under the run result
	  
		  cout << a << " ^ " << b << " = " << c;
	  
		  return 0;
		  }
	

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) in C++

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

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10; // 10 = 00000000000000000000000000001010
		  int c = ~a; // c = 11111111111111111111111111110111 = -11
	  
		  cout << "~" << a << " = " << c;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    ~10 = -11
		


worker  << (Left Shift) in C++

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

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10; // 10 = 00000000000000000000000000001010
		  int c = a << 2; // We explained how the output will be obtained under the run result
	  
		  cout << a << " << 2 = " << c;
	  
		  return 0;
		  }
	

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) in C++

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 all bit them with zero and puts them on the left.

  • If the number is less than zero, erase bits on the right, then switch all 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.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 9; // 9 = 00000000000000000000000000001001
		  int c = a >> 2; // We explained how the output will be obtained under the run result
	  
		  cout << a << " >> 2 = " << c;
	  
		  return 0;
		  }
	

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 c

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

second example

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

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  // Note "> >> " converts the number in 32 bits form
	  
		  int a = -9; // -9 = 111111111111111111111111111111000
		  int c = a >> 2; // We explained how the output will be obtained under the run result
	  
		  cout << a << " >> 2 = " << c;
	  
		  return 0;
		  }
	

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