# Feeling a lil bitstupid with this bitwise.

• 07-05-2011, 05:51 AM
demitrix
Feeling a lil bitstupid with this bitwise.
I have a function written in JavaScript that deals with unsigned longs (guess you could call them since JS has no var types). Anyhow I'm porting the project to Java and the same bitshifting operations are not working as expected. Here is an example.

JavaScript:
1509786213^3738585502=-2027172869
Java
1509786213^3738585502=2267794427

I can't get any bitshift operation to change the number so the right number. Any ideas? Thanks ahead of time.
• 07-05-2011, 06:11 AM
Junky
Your problem is that in Java by default literal values are ints. 3738585502 is larger than the largest int value. You can try making your values longs by placing an 'L' after the number: 3738585502L
• 07-05-2011, 11:22 AM
Jodokus
Quote:

JavaScript:
1509786213^3738585502=-2027172869
Seems JavaScript is choking on this one too :(giggle):
I have as book in front of me calculating 2^11213-1 (a Mersenne prime) and it needs two pages to print the result. The BigInteger class has a pow-method but only for int-arguments.
• 07-05-2011, 11:38 AM
JosAH
Quote:

Originally Posted by Jodokus
Seems JavaScript is choking on this one too :(giggle):
I have as book in front of me calculating 2^11213-1 (a Mersenne prime) and it needs two pages to print the result. The BigInteger class has a pow-method but only for int-arguments.

I think the OP means an exlusive-or operation by the '^' symbol; what I don't understand is how Java (according to the OP) calculates the expression as:

1509786213^3738585502=2267794427

I tried it but my result is negative (as the Javascript result) as well.

kind regards,

Jos

ps. only the exponent needs to be an int, that mantissa is a BigInteger so your Mersenne prime can be calculated by a BigInteger object ;-)
• 07-05-2011, 12:15 PM
dlorde
Quote:

Originally Posted by JosAH
I think the OP means an exlusive-or operation by the '^' symbol; what I don't understand is how Java (according to the OP) calculates the expression as:

1509786213^3738585502=2267794427

I tried it but my result is negative (as the Javascript result) as well.

It works for longs: 1509786213^3738585502L
• 07-05-2011, 12:34 PM
JosAH
Quote:

Originally Posted by dlorde
It works for longs: 1509786213^3738585502L

I know, but the OP didn't use longs anywhere (no trailing L to be seen ...) In my Java (1.6) the result is the same as the Javascript result mentioned by the OP.

kind regards,

Jos
• 07-05-2011, 01:07 PM
Jodokus
@Jos
Eating sand again. (Working too much with my own calculator that interprets me fine :(sweat):)
Quote:

only the exponent needs to be an int, that mantissa is a BigInteger so your Mersenne prime can be calculated by a BigInteger object ;-)
I know. The argument is the exponent, also in the book-example.
• 07-05-2011, 01:15 PM
JosAH
Quote:

Originally Posted by Jodokus
@Jos
Eating sand again. (Working too much with my own calculator that interprets me fine :(sweat):)

I know. The argument is the exponent, also in the book-example.

I use the ** operator in my language (also Python, Algol and maybe more languages have it) because the ^ operator looks so xor-y to me and I like to keep it that way ;-)

kind regards,

Jos
• 07-05-2011, 05:30 PM
demitrix
I've tried with and without longs neither way seemed to make a difference. I just stuck it in its own lil class and ran it like such
Code:

```public static void main(String[] args) { System.out.println(1509786213^3738585502L); }```
Result is always 2267794427.
• 07-05-2011, 05:37 PM
JosAH
Quote:

Originally Posted by demitrix
I've tried with and without longs neither way seemed to make a difference. I just stuck it in its own lil class and ran it like such
Code:

```public static void main(String[] args) { System.out.println(1509786213^3738585502L); }```
Result is always 2267794427.

Which is correct; the first number fits in an int (nothing is lost) and the second number is a long (the value fits in a long) while the ^ operator casts both operands to a long value and does a xor on two longs, you saw the result.

kind regards,

Jos
• 07-05-2011, 05:38 PM
demitrix
So how are others getting the correct negative result?
• 07-05-2011, 05:41 PM
JosAH
Quote:

Originally Posted by demitrix
So how are others getting the correct negative result?

Leave out the trailing L on the literal numbers and see.

kind regards,

Jos
• 07-05-2011, 05:45 PM
demitrix
Using them as literals it wont even compile, because the second number is too large. But you gave me enough insight to understand whats happening. I ended up casting the result back to an int and got the correct number. I've been in JS so long that I have to get reacquainted with strong typing. Thanks.
• 07-05-2011, 05:54 PM
JosAH
Quote:

Originally Posted by demitrix
Using them as literals it wont even compile, because the second number is too large. But you gave me enough insight to understand whats happening. I ended up casting the result back to an int and got the correct number. I've been in JS so long that I have to get reacquainted with strong typing. Thanks.

So now you did this:

Code:

`System.out.println((int)(1509786213^3738585502L));`
... which is fine because you say that a number too large to fit in an int should be treated as a long (that's what the trailing L does); the xor operator casts both operands to a long and in the end you cast the result back to an int; most likely your Javascript compiler allows literal ints to overflow silently ...

kind regards,

Jos
• 07-05-2011, 06:16 PM
demitrix
Okay, I'm really sorry to keep bugging about this but I hit another problem, -2027172869^421330478. The result I'm looking for is 2654418389, but Java gives me -1640548907. I toyed around with int vs long and couldn't seem to get it working.
• 07-05-2011, 06:38 PM
JosAH
Print out those values in binary; the classes Integer and Long have method for it; also print out the binary form of the result and see.

kind regards,

Jos
• 07-05-2011, 07:03 PM
demitrix
I understand that the results are the same in binary, but how can I get Java to handle it properly, I think what youre tryin to get me to see is that its treating the result as an int when it needs to be a long but casting doesnt work since it wont just add in the extra bits.
• 07-05-2011, 07:59 PM
JosAH
Ok, subtract the result you're expecting from the result you're getting and see the result (all in int arithmetic). Also read about sign extension.

kind regards,

Jos
• 07-05-2011, 11:19 PM
demitrix
I got everything sorted. Thanks for all the help:(clap):
• 07-06-2011, 08:11 AM
JosAH
You're welcome of course; it's a nasty little hurdle trying to understand the details of signed numbers and sign extension and (implicit) casts, but it's a nice thing when you're passed that hurdle ;-)

kind regards,

Jos