Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to find the minimum number of bits required to store an int number in C

1 Answer

0 votes
#include <stdio.h>

int countBit(int num) {
	int count = 0;
	
	if (num == 0) {
	    return 0;
	}
	
	for (int i = 0; i < 32; i++) {	
		if ( (1 << i) & num) {
			count = i;
		}
	}
	
	return ++count;
}


int main()
{
    int num = 17; // 00010001

	printf("Total number of bits required = %d\n",countBit(num));
	
	return 0;
}




/*
run:

Total number of bits required = 5

*/

 



answered Dec 19, 2023 by avibootz

Related questions

1 answer 90 views
90 views asked Apr 12, 2024 by avibootz
3 answers 199 views
1 answer 112 views
1 answer 142 views
...