Int To Long Compressor : long « Data Type « Java






Int To Long Compressor

      
//package com.myapp.util.cache.compress;

public class IntToLongCompressor {
    
    public static long pack(int x, int y) {
        long xPacked = ((long)x) << 32;
        long yPacked = y & 0xFFFFFFFFL;
        return xPacked | yPacked;
    }

    public static int unpackX(long packed) {
        return (int) (packed >> 32);
    }

    public static int unpackY(long packed) {
        return (int) (packed & 0xFFFFFFFFL);
    }



    public static void main(String[] args) {
        int x = -23664;
        int y = -6543;

        System.out.println("x=    "+Integer.toHexString(x));
        System.out.println("y=    "+Integer.toHexString(y));

        long pack = pack(x,y);
        System.out.println("pack= "+Long.toHexString(pack));

        int x2 = unpackX(pack);
        System.out.println("x2=   "+Integer.toHexString(x2));

        int y2 = unpackY(pack);
        System.out.println("y2=   "+Integer.toHexString(y2));

        testAll();
    }

    private static void testAll() {
        long pack, iteration = 0;
        int iTmp, jTmp;


        for (int i = -100000; i <= 100000; i++) {
            if (i % 100 == 0)
                System.out.println("i = "+i+ ", iterations = "+iteration);

            for (int j = -100000; j <= 100000; j++, iteration++) {
                pack = pack(i, j);
                iTmp = unpackX(pack);
                jTmp = unpackY(pack);

                if (i != iTmp || j != jTmp) {
                    throw new RuntimeException("i="+i+", j="+j);
                }
            }
        }
    }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Long class creates primitives that wrap themselves around data items of the long data type
2.Calculate factorial of integers up to this valueCalculate factorial of integers up to this value
3.Min and Max values of datatype long
4.Java long Example: long is 64 bit signed type
5.Convert Java String to Long example
6.Use toString method of Long class to convert Long into String.
7.Convert long primitive to Long object Example
8.Convert Long to numeric primitive data types example
9.Create a Long object
10.Convert String to long Example
11.Compare Two Java long Arrays Example
12.Java Sort long Array Example
13.Convert bytes to megabytes
14.Converting a String to a long type Number
15.Convert from String to long
16.Convert from long to String
17.Compute prime numbers
18.Returns the sign for long value x
19.Gets the maximum of three long values.
20.Gets the minimum of three long values.
21.A utility class for converting a long into a human readable string.
22.Add two long integers, checking for overflow.