Here you can find the source of zigzagEncode(int input)
public static int zigzagEncode(int input)
//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); } }