# Thread: Feeling a lil bitstupid with this bitwise.

1. Member
Join Date
Jul 2011
Posts
7
Rep Power
0

## 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.

2. 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

3. JavaScript:
1509786213^3738585502=-2027172869
Seems JavaScript is choking on this one too
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.

4. Originally Posted by Jodokus
Seems JavaScript is choking on this one too
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 ;-)

5. Senior Member
Join Date
Jun 2008
Posts
339
Rep Power
9
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

6. 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

7. @Jos
Eating sand again. (Working too much with my own calculator that interprets me fine )
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.

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

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

9. Member
Join Date
Jul 2011
Posts
7
Rep Power
0
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
Java Code:
```public static void main(String[] args) {
System.out.println(1509786213^3738585502L);
}```
Result is always 2267794427.

10. 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
Java 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

11. Member
Join Date
Jul 2011
Posts
7
Rep Power
0
So how are others getting the correct negative result?

12. 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

13. Member
Join Date
Jul 2011
Posts
7
Rep Power
0
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.

14. 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:

Java 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

15. Member
Join Date
Jul 2011
Posts
7
Rep Power
0
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.

16. 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

17. Member
Join Date
Jul 2011
Posts
7
Rep Power
0
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.

18. 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

19. Member
Join Date
Jul 2011
Posts
7
Rep Power
0
I got everything sorted. Thanks for all the help

20. 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•