Search This Blog

Sunday, October 5, 2014

WAP to find number of set bits in a given 32-bits number

1:  main()  
2:  {  
3:    int num = 0x1fa12cde,count = 0;  
4:    while(num)  
5:    {  
6:      if(num & 1)  
7:      {  
8:        count++;  
9:      }  
10:      num = num >>1;  
11:    }  
12:    printf("Number of set bits = %d\n",count);  
13:  }  

No comments:

Post a Comment