Results 1 to 20 of 20
 07052011, 05:51 AM #1Member
 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.
 07052011, 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
 07052011, 11:22 AM #3JavaScript:
1509786213^3738585502=2027172869
I have as book in front of me calculating 2^112131 (a Mersenne prime) and it needs two pages to print the result. The BigInteger class has a powmethod but only for intarguments.No bug ever had to calculate its fitnessfunction.
 07052011, 11:38 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
I think the OP means an exlusiveor 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 ;)The only person who got everything done by Friday was Robinson Crusoe.
 07052011, 12:15 PM #5Senior Member
 Join Date
 Jun 2008
 Posts
 339
 Rep Power
 9
 07052011, 12:34 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 07052011, 01:07 PM #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 ;)No bug ever had to calculate its fitnessfunction.
 07052011, 01:15 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 07052011, 05:30 PM #9Member
 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); }
 07052011, 05:37 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
The only person who got everything done by Friday was Robinson Crusoe.
 07052011, 05:38 PM #11Member
 Join Date
 Jul 2011
 Posts
 7
 Rep Power
 0
So how are others getting the correct negative result?
 07052011, 05:41 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 07052011, 05:45 PM #13Member
 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.
 07052011, 05:54 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
So now you did this:
Java Code:System.out.println((int)(1509786213^3738585502L));
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07052011, 06:16 PM #15Member
 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.
 07052011, 06:38 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07052011, 07:03 PM #17Member
 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.
 07052011, 07:59 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07052011, 11:19 PM #19Member
 Join Date
 Jul 2011
 Posts
 7
 Rep Power
 0
I got everything sorted. Thanks for all the help
 07062011, 08:11 AM #20
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
Similar Threads

help me in simple Bitwise operator :(
By funkygarzon in forum New To JavaReplies: 3Last Post: 08132010, 10:48 AM 
bitwise not ~ operator
By kezkez in forum New To JavaReplies: 2Last Post: 02102010, 07:49 AM 
Confusing with bitwise NOT operator
By Willi in forum New To JavaReplies: 4Last Post: 10162009, 11:06 PM 
Bitwise operations with long
By NewJavaBean in forum New To JavaReplies: 2Last Post: 04072009, 07:37 PM 
Using the bitwise operators
By Java Tip in forum java.langReplies: 0Last Post: 04172008, 07:39 PM
Bookmarks