Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions algorithms/bit-manipulation/power-of-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* power of 2 algorithm
* check wether a given integer n
* can be in a put in a form of power of 2
* by using a bitwise manipulation
* Time compexity = O(n)
* example : n = 16 can be put in the form 2^4
*/

int power_of_two(int n){
/* for edge cases */
if(n <= 0) return 0;

if ((n & (n - 1)) == 0)
{
return 1; // true
}
return 0; // false
}

/**
***EXPLAINATION
* how it checks wether an integer can be put
* in the form of power of 2 or not is
***any integer number which can be put
* in the form of power of 2 has only
* 1 bit in the binary reprentation
* example : n = 16 , 00010000 (8 bits represntation)
* so when we do AND bitwise manipulation with the
* previous number it will always give 0
* if the integer n can be kept in the form of
* power of 2 ***
*
* EXAMPLE :
* n = 16 , 00010000 (8 bits)
* n = n-1 = 15 , 00001111 (8 bits) previous value
* n & (n-1) , 00010000
* & 00001111
* 00000000
* which is 0
* so it can be put in the form of power of 2
*/