Results 1 to 20 of 20
  1. #1
    demitrix is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    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.
    No bug ever had to calculate its fitnessfunction.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Jodokus View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    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. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dlorde View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @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.
    No bug ever had to calculate its fitnessfunction.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Jodokus View Post
    @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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    demitrix is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by demitrix View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    demitrix is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    So how are others getting the correct negative result?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by demitrix View Post
    So how are others getting the correct negative result?
    Leave out the trailing L on the literal numbers and see.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    demitrix is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by demitrix View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    demitrix is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    demitrix is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    demitrix is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    I got everything sorted. Thanks for all the help

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. help me in simple Bitwise operator :(
    By funkygarzon in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 10:48 AM
  2. bitwise not ~ operator
    By kezkez in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 06:49 AM
  3. Confusing with bitwise NOT operator
    By Willi in forum New To Java
    Replies: 4
    Last Post: 10-16-2009, 11:06 PM
  4. Bitwise operations with long
    By NewJavaBean in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 07:37 PM
  5. Using the bitwise operators
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:39 PM

Posting Permissions

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