UVa 10931 – Parity

#include <iostream>

using namespace std ;

int countBits(int i){
   i = i - ((i >> 1) & 0x55555555);
   i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
   return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}

int main (){
   int n,j;
   char s[33] ;
   cin>>n;
   while (n){
       j=0;
       while (n>>j)
           s[j++] = ((n>>j)&1)+'0';


       cout<<"The parity of ";
       while (j--)
           cout.put(s[j]);

       cout<<" is "<<countBits(n)<<" (mod 2)."<<endl;
       cin>>n;
   }
   return 0;
}