#!/usr/bin/env awk
#
# If 'mode' is omitted, then returns true if all the bits are set,
# i.e., (val & mask) == mask.
# Else returns true if one of the bits set in 'mask' is also set in 'val'
#
# Parameters:
#   - val    : the value to test
#   - mask   : the mask to apply to val
#   - [mode] : whether to test for all the bits or only for one of the bits [default: all of the bits]
#
# Dependencies:
#   - None
#
# See Also:
#   - bfeq
#
# Examples:
#   - tawk 'bitisset($icmpStat, 1)' file.txt
#   - tawk 'bitisset($icmpStat, 0x1) { print }' file.txt
#   - tawk 'bitisset($tcpAnomaly, 0x00f8, 1)' file.txt

@include "hdr"

function bitisset(val, mask, mode,        _m, _v) {
    if (hdr()) return __PRIHDR__
    _m = strtonum(mask)
    _v = and(strtonum(val), _m)
    return mode ? _v : _v == _m
}
