Saturday, August 18, 2018

GETTING BITS

  • Problem Description

    Divya ask Devi 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 solution for this?
  • CODING ARENA::
  • #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;
    }
      
  • Test Case 1

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

    Input (stdin)
    32767
    
    
    Expected Output
    15

No comments:

Post a Comment