Write a program to count number of bits are set to 1 in an integer.
SOLUTION:
/* * Count number of bits are set to 1 in an integer. * Written by Shaeed Khan. * Date: 04 Aug 2014 * Version- 1.0 * Language: C */ #include < stdio.h > #include < stdlib.h > void main() { int x,n; printf("Enter a number :"); scanf("%d",&n); x=bit_count(n); printf("%d ",x); } int bit_count(int n) { int c=0; while(n>0) { c++; n=n&n-1; } return c; }
No comments:
Post a Comment