Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator

What is the bitset::none() function in C++?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The bitset::none() function in C++ is used to find whether none of the bits available in the bitset object are set.

This function is available in the bitset header file.

Syntax

The syntax of the bitset::none() function is shown below:


bool none();

Parameters

The function does not accept any parameters.

Return value

The none function returns a boolean value, where true denotes that none of the bits are set and false denotes that at least one of the bits is set.

This is the opposite of the bitset::any() function.

Code

Let’s have a look at the code:

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

int main() {
  bitset<4> b1 = 10;

  if (b1.none())
    cout << "None of the bits are set in " << b1 << endl;
  else
    cout << "Atleast one of the bits is set in " << b1 << endl;

  bitset<4> a1 = 00;

  if (a1.none())
    cout << "None of the bits are set in " << a1 << endl;
  else
    cout << "Atleast one of the bits is set in " << a1 << endl;

  return 0;
}
Use the bitset::none() function in C++

Explanation

  • In lines 1 and 2, we import the required header files.

  • In lines 6 and 13, we create bitset objects with the capacity to store 4 bits.

    These objects will contain the binary representation of the integers 10 and 00, respectively.

  • In line 8 and line 15, we call the none() function and print if none of the bits are set.

  • If at least one of the bits is set, then, in line 11 and line 18, we print the corresponding message.

In this way, we can use the bitset::none() function to check if none of the bits are set.

RELATED TAGS

c++
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring