user8193104
user8193104

Reputation:

Python bitshifting to java

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)

Upvotes: 0

Views: 178

Answers (2)

SamHoque
SamHoque

Reputation: 3154

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;
}

Upvotes: 0

Yoav Gur
Yoav Gur

Reputation: 1396

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

Upvotes: 3

Related Questions