Java Long to Byte longToByte(long x)

Here you can find the source of longToByte(long x)

Description

long To Byte

License

Apache License

Declaration

public static byte[] longToByte(long x) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static byte[] longToByte(long x) {
        if ((x & 0xFFFFFFFFFFFFFF00L) == 0L) {
            return new byte[] { (byte) x };
        } else if ((x & 0xFFFFFFFFFFFF0000L) == 0L) {
            return new byte[] { (byte) (x >> 8), (byte) x };
        } else if ((x & 0xFFFFFFFFFF000000L) == 0L) {
            return new byte[] { (byte) (x >> 16), (byte) (x >> 8), (byte) x };
        } else if ((x & 0xFFFFFFFF00000000L) == 0L) {
            return new byte[] { (byte) (x >> 24), (byte) (x >> 16), (byte) (x >> 8), (byte) x };
        } else if ((x & 0xFFFFFF0000000000L) == 0L) {
            return new byte[] { (byte) (x >> 32), (byte) (x >> 24), (byte) (x >> 16), (byte) (x >> 8), (byte) x };
        } else if ((x & 0xFFFF000000000000L) == 0L) {
            return new byte[] { (byte) (x >> 40), (byte) (x >> 32), (byte) (x >> 24), (byte) (x >> 16),
                    (byte) (x >> 8), (byte) x };
        } else if ((x & 0xFF00000000000000L) == 0L) {
            return new byte[] { (byte) (x >> 48), (byte) (x >> 40), (byte) (x >> 32), (byte) (x >> 24),
                    (byte) (x >> 16), (byte) (x >> 8), (byte) x };
        }//from w  ww.j a va  2s .c o m
        return new byte[] { (byte) (x >> 56), (byte) (x >> 48), (byte) (x >> 40), (byte) (x >> 32),
                (byte) (x >> 24), (byte) (x >> 16), (byte) (x >> 8), (byte) x };
    }
}

Related

  1. longToByte(long l)
  2. longToByte(long l)
  3. longToByte(long l)
  4. longToByte(long number)
  5. longToByte(long value)
  6. longToByte(long[] values)
  7. longToByte8(long value)