You need to write a byte at a time such as (based on java.nio.Bits)
static private long makeLong(byte b6, byte b5, byte b4,
byte b3, byte b2, byte b1, byte b0)
{
return ((((long)b6 & 0xff) << 48) |
(((long)b5 & 0xff) << 40) |
(((long)b4 & 0xff) << 32) |
(((long)b3 & 0xff) << 24) |
(((long)b2 & 0xff) << 16) |
(((long)b1 & 0xff) << 8) |
(((long)b0 & 0xff) ));
}
static long getLongL(ByteBuffer bb, int bi) {
return makeLong(bb.get(bi + 6),
bb.get(bi + 5),
bb.get(bi + 4),
bb.get(bi + 3),
bb.get(bi + 2),
bb.get(bi + 1),
bb.get(bi ));
}
private static byte long6(long x) { return (byte)(x >> 48); }
private static byte long5(long x) { return (byte)(x >> 40); }
private static byte long4(long x) { return (byte)(x >> 32); }
private static byte long3(long x) { return (byte)(x >> 24); }
private static byte long2(long x) { return (byte)(x >> 16); }
private static byte long1(long x) { return (byte)(x >> 8); }
private static byte long0(long x) { return (byte)(x ); }
static void putLongL(ByteBuffer bb, int bi, long x) {
bb.put(bi + 6, long6(x));
bb.put(bi + 5, long5(x));
bb.put(bi + 4, long4(x));
bb.put(bi + 3, long3(x));
bb.put(bi + 2, long2(x));
bb.put(bi + 1, long1(x));
bb.put(bi , long0(x));
}
In general, I avoid making micro-optimisation like this as it adds complexity for little gain IMHO. If you want to save space I suggest using something like stop bit encoding which uses one byte for every 7 bits. i.e. a long might only use 1 byte for small values but could be much larger as required.