Basic knowledge Review (ii)--stdafx.h header files and x& (X-1) operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.