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:

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
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.

Take C Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
$ 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.

advertisement
$ 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

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.