How many bits?
Problem Description
Sasha ask Diya to write a program to get minimum number of bits to store an integer number. Devi thinks a lot but she could not able to do this.Can you help her to derive a solultion for this?
CODING ARENA
#include <stdio.h>
int countBit(int);
int main()
{
int num;
scanf("%d",&num);
printf("%d\n",countBit(num));
return 0;
}
int countBit(int n)
{
int count=0,i;
if(n==0) return 0;
for(i=0; i< 32; i++)
{
if( (1 << i) & n)
count=i;
}
return ++count;
}
Test Case 1
Input (stdin)127
Expected Output
7
Test Case 2
Input (stdin)32767
Expected Output
15
No comments:
Post a Comment