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)

2 Answers2

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

Yoav Gur
  • 1,366
  • 9
  • 15
  • 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). – Yoav Gur Oct 18 '18 at 14:11
  • "Java 8 allowing unsigned int" .. what do you mean by that? –  Oct 18 '18 at 14:47
  • 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". – Yoav Gur Oct 18 '18 at 16:07
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;
}
SamHoque
  • 2,978
  • 2
  • 13
  • 43