Given a number n, find the highest power of 2 that divides n.

**Examples:**

Input : n = 48

Output : 16

Highest power of 2 that divides 48 is 16.Input : n = 5

Output : 1

Highest power of 2 that divides 5 is 1.

A **simple solution **is to try all powers of 2 one by one starting from 1, then 2, then 4 and so on.

An **efficient solution** is based on bit magic. If we take a closer look, we can notice that, we basically need to find the number that has rightmost bit set at same position as n and all other bits as 0. For example, for n = 5 (10**1**), our output is 00**1**. For n = 48 (1**1**0000), our output is 0**1**0000

*How do we find a number that has same rightmost set bit and all other bits as 0?*

We follow below steps.

Let n = 48 (00110000)

Subtract one from n, i.e., we do **n-1**. We get 47(00101111)

Do negation of (n-1), i.e., we do **~(n-1)**. We get (11010000).

Do n & **(~(n-1))**, we get 00010000 which has value 16.

Below is the implementation of above approach:

## C++

`// CPP program to find highest power ` `// of 2 that divides n. ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `int` `highestPowerOf2(` `int` `n) ` `{ ` ` ` `return` `(n & (~(n - 1))); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `n = 48; ` ` ` `cout << highestPowerOf2(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find highest power ` `// of 2 that divides n. ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `highestPowerOf2(` `int` `n) ` `{ ` ` ` `return` `(n & (~(n - ` `1` `))); ` `} ` ` ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `n = ` `48` `; ` ` ` `System.out.println(highestPowerOf2(n)); ` `} ` `} ` ` ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find highest power ` `# of 2 that divides n. ` ` ` `def` `highestPowerOf2(n): ` ` ` ` ` `return` `(n & (~(n ` `-` `1` `))) ` ` ` ` ` `#Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `48` ` ` `print` `(highestPowerOf2(n)) ` ` ` `# this code is contributed ` `# by ash264 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find highest power ` `// of 2 that divides n. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `highestPowerOf2(` `int` `n) ` `{ ` ` ` `return` `(n & (~(n - 1))); ` `} ` ` ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 48; ` ` ` `Console.Write(highestPowerOf2(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find highest power ` `// of 2 that divides n. ` ` ` `function` `highestPowerOf2(` `$n` `) ` `{ ` ` ` `return` `(` `$n` `& (~(` `$n` `- 1))); ` `} ` ` ` `// Driver Code ` `$n` `= 48; ` `echo` `highestPowerOf2(` `$n` `); ` ` ` `// This code is contributed ` `// by Sach_Code.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

16

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Highest power of 2 that divides a number represented in binary
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Highest power of 2 less than or equal to given number
- Check if given number is a power of d where d is a power of 2
- Check if a M-th fibonacci number divides N-th fibonacci number
- Find the sum of power of bit count raised to the power B
- Smallest integer > 1 which divides every element of the given array
- Largest number that divides x and is co-prime with y
- Check if the sum of digits of a number N divides it
- Minimum value that divides one number and divisible by other
- Find element in array that divides all array elements
- Find whether a given number is a power of 4 or not
- Check if a large number is divisible by a number which is a power of 2
- Program to find whether a no is power of two
- Check if bitwise AND of any subset is power of two
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Find whether a given integer is a power of 3 or not
- Count of numbers which can be made power of 2 by given operation
- Compute modulus division by a power-of-2-number
- Check if a number is power of 8 or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.