Important alert: (current site time 7/15/2013 3:37:17 PM EDT)
 

VB icon

A Simple Integer Power Function

Email
Submitted on: 6/1/2006 4:45:13 PM
By: Happy Newt  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 7166
author picture
(About the author)
 
     Non floating-point exponent. Computes the result of x^y where x and y are integers and fp arithmetic is not supported (e.g. CLDC 1.0)

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A Simple Integer Power Function
// Description:Non floating-point exponent.
Computes the result of x^y where x and y are integers and fp arithmetic is not supported (e.g. CLDC 1.0)
// By: Happy Newt
//
// Inputs:int x, int y
//
// Returns:returns x^y as a (long) number
NOTE: y is treated as an unsigned int
//
// Assumes:y is treated as UNSIGNED.
In the case where x^y is greater than Long.MAX_VALUE, the result will overflow in the same way as an integer * , + or - operation. (A wraparound effect.) So watch out for that.
This is a really tiny little function from the com.merrynewton.util.nonfp.IntMath class, from the MNMU1.0 class library
The whole library is free to use, covered by a BSD licence.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5301&lngWId=2//for details.//**************************************

// RAISE THE FIRST ARGUMENT TO THE POWER OF THE SECOND
// b will be treated as an UNSIGNED integer
 public static long power(int x,int y){
 long p=1;
 long b=((long)y)&0x00000000ffffffffL;
 // bits in b correspond to values of powerN
 // so start with p=1, and for each set bit in b, multiply corresponding table entry
 long powerN=x;
 while(b!=0){
 if((b&1)!=0) p*=powerN;
 b>>>=1;
 powerN=powerN*powerN;
 }
 return p;
 } // power


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.