module BinData::BitField

def create_clamp_code(nbits)

def create_clamp_code(nbits)
  min = 0
  max = (1 << nbits) - 1
  clamp = "val = (val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
  if nbits == 1
    # allow single bits to be used as booleans
    "val = (val == true) ? 1 : (not val) ? 0 : #{clamp}"
  else
    clamp
  end
end