A personal understanding of the power functions of SGI STL

Source: Internet
Author: User

the power function of the SGI STL is used to calculate the nth exponent of a number, such as the n power n for x.= -( -Binary10100)                          1    0   1   0   0                    -=2^4+2^2x^ -= x^ ((2^4) + (2^2)) = x^ (2^4) * x ^ (2^2) Part2 Part1template<class_TP,class_integer,class_monoidoperation>_tp __power (_tp __x, _integer __n, _monoidoperation __opr) {if(__n = =0)    returnidentity_element (__OPR); Else {     while((__n &1) ==0) {//__n >>=1;//This while loop is part1 to find out__x = __opr (__x, __x);//}_TP __result= __x;//the value of the current x is Part1__n >>=1;//Here's a _result record of part1, so here n just move right one.         while(__n! =0) {//__x = __opr (__x, __x);//ask for Part2 (if more part is also calculated in this loop, the instructions here are only for 20 powers)      if((__n &1) !=0)//__result = __opr (__result, __x);//result = result * Part2 record only if a bit with a value of 1 is encountered (result = Part1 at this time)//because only a bit is 1 to find the next part__n >>=1;//n = n/2;    }    return__result; }}Template<typename t>structMultiplies {typedef T first_argument_type;        typedef T SECOND_ARGUMENT_TYPE;      typedef T RESULT_TYPE; __host__ __device__ Toperator()(ConstT &AMP;LHS,ConstT &AMP;RHS)Const{returnLHS *RHS;} }; //End Multiplies

A personal understanding of the power functions of SGI STL

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.