# C Program to Round Floor of Integer to Next Lower Power of 2

This is a C Program to round floor of integer to next lower power of 2.

Problem Description

This C Program round floor of integer to next lower power of 2

Problem Solution

Take input from the user and performs powers operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to round floor of integer to next lower power of 2. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to round floor of integer to next lower power of 2
*/
#include <stdio.h>

int main ()
{
int num = 0;
printf("\nEnter the number: ");
scanf ("%d", &num);
num --;
num = num | (num >> 1);
num = num | (num >> 2);
num = num | (num >> 4);
num = num | (num >> 8);
num = num | (num >> 16);
num ++;
printf ("\n NEXT NUMBER LOWER TO THE POWER OF 2 : %d\n", num);
return 0;
}```
Program Explanation

1. In this C Program, We are reading a number using ‘num’ variable.
2. Take the input number from the user that number will store in num variable. First subtract the 1 from the num.
3. Next we need to perform all the bits in the num set.
For Example:

```num = num | (num >> 1); //  num = 10000 | 01000
num = num | (num >> 2); // num = 11000 | 00110
num = num | (num >> 4); // num = 11110 | 00001
num = num | (num >> 8); // num = 11111 | 00000
num = num | (num >> 16); // num = 11110 | 00000```

4. After completing all shifts num will have the value that is floor to the lower power of 2.

Runtime Test Cases

Test case 1 – Enter the number which is less than 16.

```\$ gcc next_pow.c -o next_pow
\$ ./next_pow

Enter the number: 10
NEXT NUMBER LOWER TO THE POWER OF 2 : 16```

Test case 2 – Enter the number which is less than 5.

Note: Join free Sanfoundry classes at Telegram or Youtube
```\$ gcc next_pow.c -o next_pow
\$ ./next_pow

Enter the number: 4
NEXT NUMBER LOWER TO THE POWER OF 2 : 4```

Test case 3 – Enter the number which is less than 10.

```\$ gcc next_pow.c -o next_pow
\$ ./next_pow

Enter the number: 6
NEXT NUMBER LOWER TO THE POWER OF 2 : 8```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms