Canna

Saturday, 15 June 2019

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?
  • Test Case 1

    Input (stdin)
    127
    
    
    Expected Output
    7
  • Test Case 2

    Input (stdin)
    32767
    
    
    Expected Output
    15
  • Program
  • #include <stdio.h>
    int countbit(int);
    int main()
    {
      int num;
      scanf("%d",&num);
      printf("%d",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;
    
    
    
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.