package school_project.Utils;

import school_project.Vec2;

public class Bitwise {

    /**
     * Check if the bit at pos is 1 or 0
     * @param b byte to test
     * @param pos position in b to check
     * @return true if the bit at pos is 1 or false if it is 0
     */
    public static boolean IsBitSetAt(byte b, int pos){
        pos = 7 - pos;
        return (b & (1 << pos))!= 0;
    }

    /**
     * Transform a byte (8 bit) to two Nible (4 bit) with a split in the middle
     * Exemple:
     * in = 01000101 (=69)
     * out = { 00000100, 00000101 } (={4, 5})
     *
     * @param in the byte to split
     * @return an arrya of 2 byte ret[0] = left part; ret[1] = right part
     */
    public static Vec2 ByteToNible(byte in){
        Vec2 ret = new Vec2();
        ret.x = (byte) (in >> 4);
        ret.y = (byte) (in & 15); // apply the mask '00001111'
        return ret;
    }

    /**
     * Transform 2 byte into 1 with a left part ( 4 bits ) and a right part ( 4 bits)
     * @param left first 4 bits
     * @param right last 4 bits
     * @return concatenated byte
     */
    public static byte NibbleToByte(byte left, byte right){
        return (byte) ((left << 4) | right);
    }
}
