Java Zigzag Encode zigzagEncode(int input)

Here you can find the source of zigzagEncode(int input)

Description

zigzag Encode

License

Open Source License

Declaration

public static int zigzagEncode(int input) 

Method Source Code

//package com.java2s;

public class Main {
    public static int zigzagEncode(int input) {
        // Canonical version:
        //return (input << 1) ^  (input >> 31);
        // but this is even better
        if (input < 0) {
            return (input << 1) ^ -1;
        }//from w  w w  .j  av  a  2 s .  co m
        return (input << 1);
    }

    public static long zigzagEncode(long input) {
        // Canonical version
        //return (input << 1) ^  (input >> 63);
        if (input < 0L) {
            return (input << 1) ^ -1L;
        }
        return (input << 1);
    }
}

Related

  1. zigzagEncode(byte[] data)
  2. zigZagEncode(int i)
  3. zigzagEncode(int input)
  4. zigZagEncode(long l)
  5. zigZagEncode(long n)
  6. zigzagEncode(long val)