- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Input Output Operations
- C++ Basic Input/Output
- C++ Cin
- C++ Cout
- C++ Manipulators
- Type System & Data Representation
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ Jump Statements
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Return Values
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Array Decay
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Destructors
- C++ Virtual Destructor
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Override Specifiers
- C++ Final Specifiers
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ Prototype Design Pattern
- C++ Structural Design Patterns
- C++ Facade Design Pattern
- C++ Iterator Design Pattern
- C++ Mediator Design Pattern
- C++ Memento Design Pattern
- C++ Observer Design Pattern
- C++ State Design Pattern
- C++ Strategy Design Pattern
- C++ Template Method Design Pattern
- C++ Visitor Design Pattern
- C++ Behavioural Design Pattern
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Move Semantics
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ nullptr
- C++ unordered_multiset
- C++ Chain of Responsibility
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
- C++ Command Pattern
- C++ Proxy Pattern
- C++ Useful Resources
- C++ Questions and Answers
- C++ Quick Guide
- C++ Cheatsheet
- C++ STL Tutorial
- C++ Standard Library
- C++ Useful Resources
- C++ Discussion
- C++ Online Compiler
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