0

I found a python code on github, and I need to do the same thing in java, I have almost converted it to java but I'm getting an warning saying Shift operation '>>' by overly large constant value

this is the python code that I'm trying to convert

if i > 32:
  return (int(((j >> 32) & ((1 << i))))) >> i
return (int((((1 << i)) & j))) >> i

and this is the java code I made trying to convert from the python code

if (i > 32) {
    return (j >> 32) & (1 << i) >> i;
  }
return ((1 << i) & j) >> i;

the warning is in this line (j >> 32)

7
  • 1
    What type is j? Commented Oct 18, 2018 at 13:57
  • j is an integer value Commented Oct 18, 2018 at 13:58
  • byte, short, int, or long? The exact type matters. Commented Oct 18, 2018 at 14:01
  • Possible duplicate of Bitshifting a long in Java Commented Oct 18, 2018 at 14:02
  • It is the same as doing j = 0. That's why. Commented Oct 18, 2018 at 14:02

2 Answers 2

3

Since Java's int is 32 bits (See here), shifting it 32 bits to the right leaves nothing from the original int, and therefore doesn't make much sense

Sign up to request clarification or add additional context in comments.

3 Comments

That's interesting. It might have something to do with Java 8 allowing unsigned int (even though I still find it weird to shift the entire int out).
"Java 8 allowing unsigned int" .. what do you mean by that?
It's detailed in the link in my answer: "Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers".
0

This doesn't really make sense to me because shifting 32 bits in an int leaves nothing, How ever if you want to implant the same method using long, here is the code I wrote to do so.

public int bitShift(long j, int i) {
    return i > 32 ? ((int) ((j >> 32) & ((long) (1 << i)))) >> i : ((int) (j & ((long) (1 << i)))) >> i;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.