Results 1 to 20 of 20
- 07-05-2011, 05:51 AM #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.
- 07-05-2011, 06:11 AM #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
- 07-05-2011, 11:22 AM #3
Seems JavaScript is choking on this one tooJavaScript:
1509786213^3738585502=-2027172869.gif)
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.No bug ever had to calculate its fitnessfunction.
- 07-05-2011, 11:38 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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 ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-05-2011, 12:15 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 07-05-2011, 12:34 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 07-05-2011, 01:07 PM #7
@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.only the exponent needs to be an int, that mantissa is a BigInteger so your Mersenne prime can be calculated by a BigInteger object ;-)No bug ever had to calculate its fitnessfunction.
- 07-05-2011, 01:15 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 07-05-2011, 05:30 PM #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
Result is always 2267794427.Java Code:public static void main(String[] args) { System.out.println(1509786213^3738585502L); }
- 07-05-2011, 05:37 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-05-2011, 05:38 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
So how are others getting the correct negative result?
- 07-05-2011, 05:41 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 07-05-2011, 05:45 PM #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.
- 07-05-2011, 05:54 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
So now you did this:
... 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 ...Java Code:System.out.println((int)(1509786213^3738585502L));
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-05-2011, 06:16 PM #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.
- 07-05-2011, 06:38 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-05-2011, 07:03 PM #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.
- 07-05-2011, 07:59 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-05-2011, 11:19 PM #19
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
I got everything sorted. Thanks for all the help
- 07-06-2011, 08:11 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
help me in simple Bitwise operator :(
By funkygarzon in forum New To JavaReplies: 3Last Post: 08-13-2010, 10:48 AM -
bitwise not ~ operator
By kezkez in forum New To JavaReplies: 2Last Post: 02-10-2010, 06:49 AM -
Confusing with bitwise NOT operator
By Willi in forum New To JavaReplies: 4Last Post: 10-16-2009, 11:06 PM -
Bitwise operations with long
By NewJavaBean in forum New To JavaReplies: 2Last Post: 04-07-2009, 07:37 PM -
Using the bitwise operators
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks