// Calculate the number of 1 in the binary number. Public class test_006 {public static void main (string [] ARGs) {int number = 123; int Total = 0; // number of records 1 system. out. println ("number:" + number); system. out. println ("digit binary format:" + integer. tobinarystring (number); system. out. println ("------------------"); // solution 1: Calculate the remainder of each time and 2 to see if it is an odd number. If yes, add one, the final result is the number of 1 in the binary representation. While (number> 0) {If (Number % 2 = 1) {total ++;} number/= 2;} system. out. println ("Number of 1 in solution 1:" + total); system. out. println ("------------------"); // solution 2: A loop is also used, which is simplified by the displacement operation. Number = 123; Total = 0; while (number> 0) {total + = Number & 1; number >>=1;} system. out. println ("Number of 1 in solution 2:" + total); system. out. println ("------------------"); // solution 3: Use a clever operation. Each time V & (V-1) deletes the last digit 1 in the binary representation, this technique can reduce the number of cycles. Number = 123; Total = 0; while (number> 0) {number & = (number-1); Total ++;} system. out. println ("Number of numbers 1 in solution 3:" + total); system. out. println ("------------------"); // solution 4: look-up table method. Because only 8 bits of data are available, directly create a table that contains the number of 1 in each number, and then look up the table. Complexity O (1 ). // Directly record all results in an array, and then query as needed ,}}