package com.lingniu.ingest.codec;

/** 位运算工具：从字节数组读写无符号整数。 */
public final class BitUtils {

    private BitUtils() {}

    public static int readUint8(byte[] buf, int offset) {
        return buf[offset] & 0xFF;
    }

    public static int readUint16BE(byte[] buf, int offset) {
        return ((buf[offset] & 0xFF) << 8) | (buf[offset + 1] & 0xFF);
    }

    public static long readUint32BE(byte[] buf, int offset) {
        return ((long) (buf[offset] & 0xFF) << 24)
                | ((long) (buf[offset + 1] & 0xFF) << 16)
                | ((long) (buf[offset + 2] & 0xFF) << 8)
                | (buf[offset + 3] & 0xFF);
    }

    public static void writeUint16BE(byte[] buf, int offset, int value) {
        buf[offset] = (byte) ((value >> 8) & 0xFF);
        buf[offset + 1] = (byte) (value & 0xFF);
    }

    public static void writeUint32BE(byte[] buf, int offset, long value) {
        buf[offset] = (byte) ((value >> 24) & 0xFF);
        buf[offset + 1] = (byte) ((value >> 16) & 0xFF);
        buf[offset + 2] = (byte) ((value >> 8) & 0xFF);
        buf[offset + 3] = (byte) (value & 0xFF);
    }
}
