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/d4/d38/power__of__two_8cpp.html below:

TheAlgorithms/C++: math/power_of_two.cpp File Reference

Implementation to check whether a number is a power of 2 or not.

This algorithm uses bit manipulation to check if a number is a power of 2 or not.

Algorithm

Let the input number be n, then the bitwise and between n and n-1 will let us know whether the number is power of 2 or not

For Example, If N= 32 then N-1 is 31, if we perform bitwise and of these two numbers then the result will be zero, which indicates that it is the power of 2 If N=23 then N-1 is 22, if we perform bitwise and of these two numbers then the result will not be zero , which indicates that it is not the power of 2

Note
This implementation is better than naive recursive or iterative approach.

Definition in file power_of_two.cpp.


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