1

In order to convert from int to IP String I am using approach in Going from 127.0.0.1 to 2130706433, and back again

private static final byte BYTE_MASK = (byte)0xff;

protected byte[] unpack(int bytes) {
    return new byte[] {
        (byte)((bytes >>> 24) & BYTE_MASK),
        (byte)((bytes >>> 16) & BYTE_MASK),
        (byte)((bytes >>>  8) & BYTE_MASK),
        (byte)((bytes       ) & BYTE_MASK)
   };
}

But FindBugs in Eclipse generates bugs: INT_VACUOUS_BIT_OPERATION.

INT_VACUOUS_BIT_OPERATION: bit operations that don't do any meaningful work.

Why is that and how to fix it?

Community
  • 1
  • 1
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101

1 Answers1

4

I suspect it's because you don't need the & BYTE_MASK if you're also casting to byte. I'm assuming that BYTE_MASK is 0xff... in which case it's basically pointless. Just casting will have the same effect.

From section 5.1.3 of the JLS:

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194