import struct


def get_flag(alignment: bytes) -> int:
    """
    Extract the alignment flag from a BAM alignment

    Parameters
    ----------
    alignment : bytes
        A byte string of a bam alignment entry in raw binary format

    Returns
    -------
    int
        The alignment flag

    Notes
    -----
    Flag values can be tested on raw BAM alignment with pylazybam.bam.is_flag()
    Common flag values are available from pylazybam.bam.FLAGS

    >>> print(pylazybam.bam.FLAGS)
    {"paired": 0x1,
    "aligned": 0x2,
    "unmapped": 0x4,
    "pair_unmapped": 0x8,
    "forward": 0x40,
    "reverse": 0x80,
    "secondary": 0x100,
    "qc_fail": 0x200,
    "duplicate": 0x400,
    "supplementary": 0x800,}

    See https://samtools.github.io/hts-specs/SAMv1.pdf for details.

    """
    return struct.unpack("<H", alignment[18:20])[0]