Lets see if we can turn this into something that uses a macro or another function call to get rid of all the copy/paste code.
```erlang
-define (write_nullable_array (LwesType,Guard,BinarySize,BinaryType, V),
   Len = length (V),
   {Bitset, Data} = lists:foldl (
       fun
         (undefined, {BitAccum, DataAccum}) ->
           {<<0:1, BitAccum/bitstring>>, DataAccum};
         (X, {BitAccum, DataAccum}) when Guard (X) ->.
           {<<1:1, BitAccum/bitstring>>,
             <<DataAccum/binary, X:BinarySize/BinaryType>>};
         (_, _) -> erlang:error (badarg)
       end, {<<>>, <<>>}, V),
    Lwes_Bitset_Bin = lwes_bitset_rep (Len, Bitset),
    <<LwesType:8/integer-unsigned-big,
      Len:16/integer-unsigned-big, Len:16/integer-unsigned-big,
      Lwes_Bitset_Bin/binary, Data/binary>>
    ).

bin_test (Value) ->
  ?write_nullable_array(?LWES_TYPE_U_INT_16,
                        ?is_uint16,
                        32,
                        integer-signed-big,
                        Value).