mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-30 16:06:24 +08:00
[fix/feat]: remove memory leak in avltree.cpp (#2429)
* fix: remove memory leak by adding deleteAllNodes * clang-format and clang-tidy fixes for9d76f8bd* docs: explain usage of standard headers * docs: use doxygen syntax * docs: document parameters of the functions * style: use proper spelling * style: simplify logic in deleteNode * docs: add missing [in] * docs: add missing slashes * docs: document `main` Co-authored-by: David Leal <halfpacho@gmail.com> * updating DIRECTORY.md * clang-format and clang-tidy fixes forc852f62d--------- Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Find whether a given number is power of 2]
|
||||
* (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/) implementation
|
||||
* (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/)
|
||||
* implementation
|
||||
*
|
||||
* @details
|
||||
* We are given a positive integer number. We need to check whether the number is power of
|
||||
* 2 or not.
|
||||
* We are given a positive integer number. We need to check whether the number
|
||||
* is power of 2 or not.
|
||||
*
|
||||
* A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
|
||||
* set bit in computer terms.
|
||||
@@ -27,16 +28,16 @@ namespace bit_manipulation {
|
||||
* @param n is the number who will be checked
|
||||
* @returns either true or false
|
||||
*/
|
||||
bool isPowerOfTwo(
|
||||
std ::int64_t n) { // int64_t is preferred over int so that
|
||||
// no Overflow can be there.
|
||||
bool isPowerOfTwo(std ::int64_t n) { // int64_t is preferred over int so that
|
||||
// no Overflow can be there.
|
||||
|
||||
return n > 0 && !(n & n - 1); // If we subtract a power of 2 numbers by 1
|
||||
// then all unset bits after the only set bit become set; and the set bit becomes unset.
|
||||
return n > 0 && !(n & n - 1); // If we subtract a power of 2 numbers by 1
|
||||
// then all unset bits after the only set bit become set; and the set bit
|
||||
// becomes unset.
|
||||
|
||||
// If a number n is a power of 2 then bitwise and of n-1 and n will be zero.
|
||||
// The expression n&(n-1) will not work when n is 0.
|
||||
// To handle this case also, our expression will become n& (!n&(n-1))
|
||||
// The expression n&(n-1) will not work when n is 0.
|
||||
// To handle this case also, our expression will become n& (!n&(n-1))
|
||||
}
|
||||
} // namespace bit_manipulation
|
||||
|
||||
|
||||
Reference in New Issue
Block a user