Syntax of std::has_single_bit() in C++

std::has_single_bit(num)

Parameters of has_single_bit()

This function accepts a single parameter:

  • num: It is an unsigned integer type value that we want to check.

Return Value has_single_bit()

The std::has_single_bit function return:

  • true if x is an integral power of two(has exactly one bit set in its binary representation).
  • Otherwise, it returns false.

std::has_single_bit in C++ 20

In C++ 20, the std::has_single_bit function is a predefined function that is used to check if a given value is an integral power of two or not. This function is defined in the <bit> header file.

The std::has_single_bit() function works by checking whether a given integer has exactly one bit set to ‘1’ in its binary representation because the numbers that are powers of two have only one bit set in their binary form. For example, 2 is 10 in binary, 4 is 100, 8 is 1000 and so on.

Similar Reads

Syntax of std::has_single_bit() in C++

std::has_single_bit(num)...

Example of std::has_single_bit in C++

Input:Num: 4Output:4 has a single bit set.Explanation : Binary representation of 4 is 0100 which has exactly one set bit. Therefore, 4 = 2^2 which is a integral power of two....

Contact Us