One of the C + + algorithm sentences infers that an integer is not a 2 integer

Source: Internet
Author: User

Idea: An integer is assumed to be 2 of the whole number of squares, then its binary representation has only one is 1, and all the other bits are 0. Subtract this integer from the integer minus 1 and then proceed with the operation. So this whole number is one of the only

1 becomes 0, and this integer becomes 0;

Code:

Is2.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace Std;bool is2 (int n) {return! (n& (n-1));} int _tmain (int argc, _tchar* argv[]) {bool b = IS2 (2); Cout<<b<<endl;bool C = Is2 (3); Cout<<c<<endl ; GetChar (); return 0;}

One of the C + + algorithm sentences infers that an integer is not a 2 integer

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.