// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.hytalegenerator.framework.math;

import javax.annotation.Nonnull;

public class BitConverter
{
    public static void main(final String[] args) {
        System.out.println("LONG TEST:");
        for (int i = -4; i < 10; ++i) {
            System.out.println();
            System.out.print("INPUT [" + i + "] -> BINARY -> [");
            final boolean[] bitArray;
            final boolean[] output = bitArray = toBitArray((long)i);
            for (final boolean bit : bitArray) {
                System.out.print(bit ? "1" : "0");
            }
            System.out.print("] -> DECIMAL -> [" + toLong(output));
        }
        System.out.println();
        System.out.println("INT TEST:");
        for (int i = -4; i < 10; ++i) {
            System.out.println();
            System.out.print("INPUT [" + i + "] -> BINARY -> [");
            final boolean[] bitArray2;
            final boolean[] output = bitArray2 = toBitArray(i);
            for (final boolean bit : bitArray2) {
                System.out.print(bit ? "1" : "0");
            }
            System.out.print("] -> DECIMAL -> [" + toInt(output));
        }
        System.out.println();
        System.out.println("BYTE TEST:");
        for (int i = -4; i < 10; ++i) {
            System.out.println();
            System.out.print("INPUT [" + i + "] -> BINARY -> [");
            final boolean[] bitArray3;
            final boolean[] output = bitArray3 = toBitArray((byte)i);
            for (final boolean bit : bitArray3) {
                System.out.print(bit ? "1" : "0");
            }
            System.out.print("] -> DECIMAL -> [" + toByte(output));
        }
        System.out.println();
    }
    
    public static boolean[] toBitArray(final long number) {
        final byte PRECISION = 64;
        final boolean[] bits = new boolean[64];
        long position = 1L;
        for (byte i = 63; i >= 0; --i) {
            bits[i] = ((number & position) != 0x0L);
            position <<= 1;
        }
        return bits;
    }
    
    public static boolean[] toBitArray(final int number) {
        final byte PRECISION = 32;
        final boolean[] bits = new boolean[32];
        int position = 1;
        for (byte i = 31; i >= 0; --i) {
            bits[i] = ((number & position) != 0x0);
            position <<= 1;
        }
        return bits;
    }
    
    public static boolean[] toBitArray(final byte number) {
        final byte PRECISION = 8;
        final boolean[] bits = new boolean[8];
        byte position = 1;
        for (byte i = 7; i >= 0; --i) {
            bits[i] = ((number & position) != 0x0);
            position <<= 1;
        }
        return bits;
    }
    
    public static long toLong(@Nonnull final boolean[] bits) {
        final byte PRECISION = 64;
        if (bits.length != 64) {
            throw new IllegalArgumentException("array must have length 64");
        }
        long position = 1L;
        long number = 0L;
        for (byte i = 63; i >= 0; --i) {
            if (bits[i]) {
                number += position;
            }
            position <<= 1;
        }
        return number;
    }
    
    public static int toInt(@Nonnull final boolean[] bits) {
        final byte PRECISION = 32;
        if (bits.length != 32) {
            throw new IllegalArgumentException("array must have length 32");
        }
        int position = 1;
        int number = 0;
        for (byte i = 31; i >= 0; --i) {
            if (bits[i]) {
                number += position;
            }
            position <<= 1;
        }
        return number;
    }
    
    public static int toByte(@Nonnull final boolean[] bits) {
        final byte PRECISION = 8;
        if (bits.length != 8) {
            throw new IllegalArgumentException("array must have length 8");
        }
        byte position = 1;
        byte number = 0;
        for (byte i = 7; i >= 0; --i) {
            if (bits[i]) {
                number += position;
            }
            position <<= 1;
        }
        return number;
    }
}
