Q:

C program to get minimum number of bits to store an integer number

0

C program to get minimum number of bits to store an integer number

Given an integer number and we have to find the total number of minimum bit(s) which can be used to store given integer number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

/*Program to get minimum number of bits to store an integer number.*/

#include <stdio.h>

/*function declaration
	* name		: countBit
	* Desc		: to get bits to store an int number
	* Parameter	: int 
	* return	: int 
*/
int countBit(int);
int main()
{
	int num;
	printf("Enter an integer number :");
	scanf("%d",&num);

	printf("Total number of bits required = %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;
}

Output

First run:
Enter an integer number :127
Total number of bits required = 7

Second run:
Enter an integer number :13
Total number of bits required = 4

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C solved programs/examples on Bitwise Operators

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to demonstrate example of right shift (&... >>
<< C program to swap two bits of a byte...