Here you can find the source of longToSortableBytes(long value, byte[] result, int offset)
public static void longToSortableBytes(long value, byte[] result, int offset)
//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; } }