Selected Reading

C++ Bitset::operator!= Function



The C++ std::bitset::operator!= is a binary operator that tests whether two bitset objects are not equal.

The inequality operator (!=) compares each bit of the two variables and returns true if they are not equal and false otherwise. It starts the check from the rightmost (least significant) bit to the leftmost (most significant) bit.

A binary operator takes two operands to perform a specific operation, such as addition, subtraction, multiplication, division, or comparison. It is denoted by a symbol or a keyword, such as +, -, *, /, and !=.

Syntax

Following is the syntax for std::bitset::operator!= −

bool operator!= (const bitset& other) const;

Parameters

other − Another bitset object.

Return value

Returns true if both bitsets are not equal; otherwise false.

Example 1

The following example shows the usage of std::bitset::operator!= by comparing two bitsets with different values.

We are creating two bitsets "b1" and "b2" with binary value of "1010" and "0101" respectively. We are then checking if they are not equal using the inequality operator. After that we left-shift bitset "b2" by one position and then check if "b1" and "b2" are not equal.

#include <iostream>
#include <bitset>
using namespace std;

int main(void) {
   bitset<4> b1("1010");
   bitset<4> b2("1110");
   if (b1 != b2)
      cout << "Both bitsets are not equal." << endl;
   b1 = b2;
   if ((b1 != b2))
      cout << "Both bitsets are equal." << endl;
   else
      cout << "Both bitsets are equal." << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Both bitsets are not equal.
Both bitsets are equal.

Example 2

Here, we are trying to compare two bitsets with same values using operator!=.

We are creating two bitsets "a" and "b" with the same values of "1111". Then, we are checking if they are not equal using the inequality operator.

#include <iostream>
#include <bitset>
using namespace std;

int main() {
   bitset<4> a("1111");
   bitset<4> b("1111");
   if (a != b) {
      cout << "a and b are not equal\n";
   } else {
      cout << "a and b are equal\n";
      return 0;
   }
}

Output

If we run the above code it will generate the following output −

a and b are equal

Example 3

Now, we are using operator!= to compare two bitsets in a loop.

In the following example we are creating two bitset objects "a" and "b" with a size of "4" bits each and setting their values to "1010" and "0101" respectively. We are then iterating over each bit and checking if the corresponding bits in "a" and "b" are not equal.

#include <iostream>
#include <bitset>
using namespace std;

int main() {
   bitset<4> a("1010");
   bitset<4> b("0101");
   for (int i = 0; i < 4; i++) {
      if (a[i] != b[i]) {
         cout << "bit " << i << " is not equal\n";
      } else {
         cout << "bit " << i << " is equal\n";
      }
   }
}

Output

Following is an output of the above code −

bit 0 is not equal
bit 1 is not equal
bit 2 is not equal
bit 3 is not equal

Example 4

In here, we are comparing a bitset to an integer value using operator!=.

We are creating bitset object "a" with a size of "8" bits and setting its value to "10101010". We are also defining an integer "b" with a value of "42", and then checking if "a" is not equal to "b" using the inequality operator.

#include <iostream>
#include <bitset>
using namespace std;

int main() {
   bitset<8> a("10101010");
   int b = 42;
   if (a != b) {
      cout << "a and b are not equal" << endl;
   } else {
      cout << "a and b are equal" << endl;
   }
  return 0;
}

Output

Since the binary value of 'b' is different from 'a', we get the output as shown below −

a and b are not equal
Advertisements