Math operations not working!

Hi,

I'm running java on a Mac using OSX and XCode for an IDE. When I try and use the 'to the power' operation it behaves in an unexpected manner.

int minbit = Size - 1;

if(minbit < 0) minbit = 0;

int Min = -1*2^minbit;

int Max = (2^minbit) - 1;

System.out.println("Size = " + Size +", minbit = " + minbit +", Min = " + Min +", Max = " + Max);

return Raw >= Min && Raw <= Max;

Gives the result:

Size = 2, minbit = 1, Min = -1, Max = 2

Size = 3, minbit = 2, Min = -4, Max = -1

Size = 4, minbit = 3, Min = -3, Max = 0

I would expect:

Size = 2, minbit = 1, Min = -2, Max = 1

Size = 3, minbit = 2, Min = -4, Max = 3

Size = 4, minbit = 3, Min = -8, Max = 7

I've no idea what's going on. I thought may be it was the character I was using for the 'power of' sign but I've tried copying it of the web....

Any help'd be appreciated... I'm using a for loop at the moment which is far from ideal.

[1289 byte] By [abc.mikeya] at [2007-11-26 18:46:14]
# 1

^ is not the "power of" operator in Java. You must be a VB programmer or something.

^ is the bitwise exclusive or operator:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

If you want to raise a value to a power, you need the Math.power() function.

%

duffymoa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 2

Like this:

package cruft;

/**

* Power

* User: Michael

* Date: Feb 17, 2007

* Time: 8:58:11 AM

*/

public class Power

{

public static void main(String[] args)

{

double x = ((args.length > 0) ? Double.parseDouble(args[0]) : 2);

double n = ((args.length > 1) ? Double.parseDouble(args[1]) : 3);

System.out.println("x = " + x);

System.out.println("n = " + n);

System.out.println("pow(x, n) = " + Math.pow(x, n));

}

}

%

duffymoa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 3

> If you want to raise a value to a power, you need the Math.power() function.

Ever so true but maybe in this particular case '2**x' for 'x >= 0' the expression

'1<<x' would be more efficient.

kind regards,

Jos (>< nerd ;-)

ps. silly forum software; always goofing the angular brackets ;-)

JosAHa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes I used to be a VB programer and I've forgotten my Java syntax because I've not used it for years.

In my defence the webpage I looked up said ^ was to the power in Java - must have been talking about something else though.

OK, the Maths function is useful to know. I think I might use the bit shift operator though.

Thanks,

Mikey

abc.mikeya at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 5
> Ever so true but maybe in this particular case '2**x'> for 'x >= 0' the expression '1<<x' would be more efficient.Jos, your FORTRAN roots are still showing... 8)%>
duffymoa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 6

> > Ever so true but maybe in this particular case '2**x'

> > for 'x >= 0' the expression '1<<x' would be more efficient.

>

> Jos, your FORTRAN roots are still showing... 8)

:-) ok, you win:/**

* some babble here about 1<<x being equal to 2**x

* @parameter power some more babble about 2**x

* @return and even more babbling about 2**x

*/

public final int raiseTwoToThePowerOf(int power) {

assert power >= 0 : "not a non-negative power: "+power;

int resultOfTwoRaisedToThePowerOfANumber= 1<<power;

return resultOfTwoRaisedToThePowerOfANumber;

}

kind regards,

Jos ;-)

ps. stupid forum sofware ...>

JosAHa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 7
> In my defence the webpage I looked up said ^ was to> the power in JavaHard to believe. Where is this wonderful Web page?
ejpa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 8
> > In my defence the webpage I looked up said ^ was to> > the power in Java> > Hard to believe. Where is this wonderful Web page?There is no statement so stupid that it cannot be found somewhere on the Internet.
DrClapa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...
# 9
You're right, probably someone is already referring to this page as evidence ... but OTOH one may have one's doubts as to the OP's veracity in this matter. Sounded like a dog-ate-my-homework to me.
ejpa at 2007-7-9 6:20:08 > top of Java-index,Java Essentials,Java Programming...