This C Program uses bitwise operations to round(floor of) an integer to next lower multiple of 2.
Here is source code of the C Program to use bitwise operations to round(floor of) an integer to next lower multiple of 2. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to use Bitwise Operations to Round(floor of) an Integer
* to next Lower Multiple of 2
*/
#include <stdio.h>
void main()
{
int x = 1, i, n;
printf("enter the number :");
scanf("%d", &n);
/* for positive values */
if (n > 0)
{
for (; x <= n >> 1;)
{
x = x << 1;
}
n = x;
}
/* for negative values */
else
{
n = ~n;
n = n + 1;
for (; x <= n >> 1;)
{
x = x << 1;
}
x = x << 1;
x = ~x;
x = x + 1;
n = x;
}
printf("%d", n);
}
$ cc bit10.c $ a.out enter the number :9 8 $ a.out enter the number :44 32 $ a.out enter the number :-20 -32 $ a.out enter the number :-84 -128
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice BCA MCQs
- Buy C Books
- Apply for C Internship
- Buy Computer Science Books
- Practice Computer Science MCQs