| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
/** |
| 4 |
* Class for miscellaneous helper methods. |
| 5 |
*/ |
| 6 |
public class SmileUtil |
| 7 |
{ |
| 8 |
public static int zigzagEncode(int input) { |
| 9 |
// Canonical version: |
| 10 |
//return (input << 1) ^ (input >> 31); |
| 11 |
// but this is even better |
| 12 |
if (input < 0) { |
| 13 |
return (input << 1) ^ -1; |
| 14 |
} |
| 15 |
return (input << 1); |
| 16 |
} |
| 17 |
|
| 18 |
public static int zigzagDecode(int encoded) { |
| 19 |
// canonical: |
| 20 |
//return (encoded >>> 1) ^ (-(encoded & 1)); |
| 21 |
if ((encoded & 1) == 0) { // positive |
| 22 |
return (encoded >>> 1); |
| 23 |
} |
| 24 |
// negative |
| 25 |
return (encoded >>> 1) ^ -1; |
| 26 |
} |
| 27 |
|
| 28 |
public static long zigzagEncode(long input) { |
| 29 |
// Canonical version |
| 30 |
//return (input << 1) ^ (input >> 63); |
| 31 |
if (input < 0L) { |
| 32 |
return (input << 1) ^ -1L; |
| 33 |
} |
| 34 |
return (input << 1); |
| 35 |
} |
| 36 |
|
| 37 |
public static long zigzagDecode(long encoded) { |
| 38 |
// canonical: |
| 39 |
//return (encoded >>> 1) ^ (-(encoded & 1)); |
| 40 |
if ((encoded & 1) == 0) { // positive |
| 41 |
return (encoded >>> 1); |
| 42 |
} |
| 43 |
// negative |
| 44 |
return (encoded >>> 1) ^ -1L; |
| 45 |
} |
| 46 |
} |