Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.infinispan.commons.io;
 
 
 import static org.infinispan.commons.io.UnsignedNumeric.readUnsignedInt;
import static org.infinispan.commons.io.UnsignedNumeric.writeUnsignedInt;

Variable length encoding for signed numbers, using the ZigZag technique

Author(s):
gustavonalle
Since:
8.0
See also:
https://developers.google.com/protocol-buffers/docs/encoding.types
public final class SignedNumeric {
   private SignedNumeric() {
   }
   public static int readSignedInt(ObjectInput inthrows IOException {
      return decode(readUnsignedInt(in));
   }
   public static int readSignedInt(InputStream inthrows IOException {
      return decode(readUnsignedInt(in));
   }
   public static void writeSignedInt(ObjectOutput outint ithrows IOException {
      writeUnsignedInt(outencode(i));
   }
   public static void writeSignedInt(OutputStream outint ithrows IOException {
      writeUnsignedInt(outencode(i));
   }
   public static int decode(int vint) {
      return (vint & 1) == 0 ? vint >>> 1 : ~(vint >>> 1);
   }
   public static int encode(int vint) {
      return (vint << 1) ^ (vint >> 31);
   }
New to GrepCode? Check out our FAQ X