Quickly determine whether the power of Npower is 2

Source: Internet
Author: User
Tags mathematical functions

Today, I think about how to quickly determine the power of Npower 2. Here is a summary.

I. The fastest way:

(Number & Number -   1 ) =   0

Cause: Because the Npower of 2 is converted to binary 10 ...... 0 (except 0 ). And the number of digits-1 on the top. The result is 0. For example. The binary value of 8 is 1000, and the binary value of 8-1 = 7 is 111. The result of the two phases is 0. The calculation is as follows.

1000
& 0111
----- --
0000

 Code:

// Get a number from textbox
Long Number =   Long . Parse (txbnumber. Text );

///   <Summary>
/// Use "and"
///   </Summary>
Private   Void Useandmethod ()
{
If (Number = 1 )
{
Response. Write ( " No " );
Return ;
}
String Result = (Number & Number -   1 ). Tostring ();
If (Number & Number -   1 ) =   0 )
{
Response. Write ( " Yes " );
}
Else
{
Response. Write ( " No " );
}
}

 

2. Use mathematical functions (not as good as the first method)

// Get a number from textbox
Long Number =   Long . Parse (txbnumber. Text );

///   <Summary>
/// Calculation using math. Log Method
///   </Summary>
Private   Void Usemathlog ()
{
Datetime dtstart = Datetime. now;
Double Power = Math. Log (number, 2 );
Long Result =   0 ;
If (Int64.tryparse (power. tostring (), Out Result ))
{
Datetime dtend = Datetime. now;
Response. Write (number +   " Yes 2 "   + Power +   " Power, time spent is "   + Datediff (dtend, dtstart ));
}
Else
{
Datetime dtend = Datetime. now;
Response. Write (number +   " Not the Npower of 2, "   +   " The power of proximity is: "   + Power +   " , The time spent is "   + Datediff (dtend, dtstart ));
}
}

 

// Computing Time
Private   String Datediff (datetime datetimeend, datetime datetimestart)
{
String Datediff =   Null ;
Timespan ts1 =   New Timespan (datetimeend. ticks );
Timespan ts2 =   New Timespan (datetimestart. ticks );
Timespan TS = Ts1.subtract (ts2). duration ();
Datediff = TS. totalmilliseconds. tostring () +   " Millisecond " ;
Return Datediff;
}

 

There are otherAlgorithm. Only two are listed here.

 

Related Article

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.