package com.bpa.scalage.HBL.DataUtil;



import com.bpa.scalage.HBL.Enum.DataFormat;

import java.nio.ByteBuffer;

public class ShortLib {
    public static short ToShort(byte[] bytes, DataFormat df) {
        byte[] res=  new ByteTransDataFormat().ByteTransDataFormat2(bytes,df,0);
        short result= ByteBuffer.wrap(res).getShort();
        return result;
    }

    public static short[] ToShorts(byte[] bytes, DataFormat df) {
        byte[] tempBytes=new byte[bytes.length];
        if (bytes.length%2!=0){ tempBytes=new byte[bytes.length+1]; }
        for (int i=0; i<bytes.length; i++){tempBytes[i]=bytes[i];}
        short[] result = new short[tempBytes.length/2];
        for (int i=0; i<result.length; i++){
            byte[] res=  new ByteTransDataFormat().ByteTransDataFormat2(tempBytes,df,i*2);
            result[i]=ByteBuffer.wrap(res).getShort();
        }
        return result;
    }

    public static boolean GetBit(short value,int offset)
    {
        if (offset > 16 || offset < 1) return false;
        return (value & 1 << offset - 1) == 0 ? false : true;
    }

    public static short SetBit(short value,int offset, boolean val)
    {
        if (offset > 16 || offset < 1) return value;
        short ConvertValue = (short)(1 << offset - 1);
        return (short)(val ? value | ConvertValue : value & ~ConvertValue);
    }
}