This C Program uses function to return the MSB position of unsigned integer.
Here is source code of the C Program use function to return the MSB position of unsigned integer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program that uses Function to return MSB position of unsigned Integer
*/
#include <stdio.h>
#define NUM_BITS_INT 32
int int_msb_position(int n);
void main()
{
int n, pos;
printf("Enter a number : ");
scanf("%d", &n);
pos = int_msb_position(n);
printf("\nPosition of MSB bit = %d\n", NUM_BITS_INT - (pos + 1));
}
/* Function to find the MSB bit position */
int int_msb_position(int n)
{
int i = 0, bit;
while (i < NUM_BITS_INT)
{
bit = n & 0x80000000;
if (bit == -0x80000000)
{
bit = 1;
}
if (bit == 1)
break;
n = n << 1;
i++;
}
return i;
}
$ cc bit24.c $ a.out Enter a number : 127 Position of MSB bit = 6 Enter a number : 259 Position of MSB bit = 8 Enter a number : 5 Position of MSB bit = 2
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 Computer Science Books
- Apply for Computer Science Internship
- Buy C Books
- Practice Computer Science MCQs