I have not written a C + + program for a long time today, using VS2013 to create a new empty console program, the result is automatically generated header file and Main method.
Understand the meaning and usage of the stdafx.h header file.
Stdafx:standard Application Framework Extended, is a precompiled file that will be used in the project to pre-compile some standard MFC header files, when the project is compiled again, you do not need to compile the header file, you can directly use the results of the compilation, It can speed up compilation and save program run time.
x& (X-1), the last 1 in X can be set to 0;
There are two ways to use x& (x-1): 1, to find the number of digits in X 1, and 2 to determine if X is a 2 n-th square;
Specific code:
ConsoleApplication2.cpp: Main project file. #include "stdafx.h" #include "iostream" using namespace system;using namespace std;//determine that x contains 1 of the number of int countonesum (int x) { int count = 0;while (x > 0) {x = (x& (x-1));//to 0 for the last 1 in X, the number of 1 for execution and operation; count++;} Return count;};/ /Determine if a number is 2 N, is 2 N, then the binary contains only one 1 and several 0,x-1 can be reversed, so if the & operation is 0, then 2 of the n-th square. int functwo (int x) {if (x& (x-1) = = 0) return 1;elsereturn 0;} int main (array<system::string ^> ^args) {int x = 9999;cout << countonesum (x); return 0;}
Basic knowledge Review (ii)--stdafx.h header files and x& (X-1) operations