Java Long to longToSortableBytes(long value, byte[] result, int offset)

Here you can find the source of longToSortableBytes(long value, byte[] result, int offset)

Description

Encodes an long value such that unsigned byte order comparison is consistent with Long#compare(long,long)

License

Apache License

Declaration

public static void longToSortableBytes(long value, byte[] result, int offset) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .  j a  v a2s  . c om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /** 
     * Encodes an long {@code value} such that unsigned byte order comparison
     * is consistent with {@link Long#compare(long, long)}
     * @see #sortableBytesToLong(byte[], int)
     */
    public static void longToSortableBytes(long value, byte[] result, int offset) {
        // Flip the sign bit so negative longs sort before positive longs:
        value ^= 0x8000000000000000L;
        result[offset] = (byte) (value >> 56);
        result[offset + 1] = (byte) (value >> 48);
        result[offset + 2] = (byte) (value >> 40);
        result[offset + 3] = (byte) (value >> 32);
        result[offset + 4] = (byte) (value >> 24);
        result[offset + 5] = (byte) (value >> 16);
        result[offset + 6] = (byte) (value >> 8);
        result[offset + 7] = (byte) value;
    }
}

Related

  1. LongToOctString(final long value)
  2. longToOther(Object val, Class dest)
  3. longToPrefixCoded(final long val, final int shift, final char[] buffer)
  4. longToRegisters(long v)
  5. longToRemoteAddrTo(long remoteAddr)
  6. longToStringAddress(long addr)
  7. longToStringWithZeroFill(long longValue, int width)
  8. longToTimeString(long time)
  9. longToX(double longitudeDegrees, double radius)