A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/dc/d6d/power__of__2_8cpp_source.html below:

TheAlgorithms/C++: bit_manipulation/power_of_2.cpp Source File

Go to the documentation of this file. 32bool isPowerOfTwo

(std ::int64_t n) {

// int64_t is preferred over int so that 33 // no Overflow can be there. 35 return

n > 0 && !(n & n - 1);

// If we subtract a power of 2 numbers by 1 36 // then all unset bits after the only set bit become set; and the set bit 39 // If a number n is a power of 2 then bitwise and of n-1 and n will be zero. 40 // The expression n&(n-1) will not work when n is 0. 41 // To handle this case also, our expression will become n& (!n&(n-1)) 43

}

// namespace bit_manipulation 54 // n = 13 return false 58 // n = 15 return false 62 // n = 97 return false 64 // n = 1024 return true 66

std::cout <<

"All test cases successfully passed!"

<< std::endl;

73 test

();

// run self-test implementations

bool isPowerOfTwo(std ::int64_t n)

The main function implements check for power of 2.

static void test()

Self-test implementations.

int main()

Main function.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4