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.
Related Posts:
- Apply for Computer Science Internship
- Check C Books
- Apply for C Internship
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs