#!/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($tcpAggrAnomaly, 0x00f8, 1)' file.txt

function bitisset(val, mask, mode,        _v) {
    _v = and(strtonum(val), mask)
    if (mode) return _v
    else return _v == strtonum(mask)
}
