Could anyone explain why converting to string and then checking each character not work? int result = 0; string mystring = n.ToString(); for(int i =0;i<mystring.Length;i++) { if (mystring[i] == '1') result++; }