public static int Encode_Bitstring(ref Byte[] apdu, ref BACNET_BIT_STRING bit_string, int pos)
        {
            int  len = 0;
            Byte remaining_used_bits = 0;
            Byte used_bytes          = 0;
            Byte i = 0;

            if (bit_string.bits_used == 0)
            {
                apdu[pos] = 0;
                len++;
            }
            else
            {
                used_bytes          = bit_string.bytes_used();
                remaining_used_bits = (Byte)(bit_string.bits_used - ((used_bytes -
                                                                      1) * 8));
                apdu[pos + len++] = (Byte)(8 - remaining_used_bits);
                for (i = 0; i < used_bytes; i++)
                {
                    apdu[pos + len++] = bit_string.byte_reverse_bits(bit_string.bitstring_octet(i));
                }
            }
            return(len);
        }
        public static int Decode_Context_Bitstring(ref Byte[] apdu, Byte tag_number, ref BACNET_BIT_STRING bit_string, int pos)
        {
            UInt32 len_value = 0;
            int    len       = 0;

            len += Decode_Tag_number_and_Value(ref apdu, ref tag_number, ref len_value, pos + len);
            len += Decode_Bitstring(ref apdu, len_value, ref bit_string, pos + len);
            return(len);
        }
        public static int Encode_Context_Bitstring(ref Byte[] apdu, Byte tag_number, ref BACNET_BIT_STRING bit_string, int pos)
        {
            int    len = 0;
            UInt32 bit_string_encoded_length = 1;

            bit_string_encoded_length += bit_string.bytes_used();
            len  = Encode_Tag(ref apdu, tag_number, true, bit_string_encoded_length, pos + len);
            len += Encode_Bitstring(ref apdu, ref bit_string, pos + len);


            return(len);
        }
        public static int Decode_Bitstring(ref Byte[] apdu, UInt32 len_value, ref BACNET_BIT_STRING bit_string, int pos)
        {
            int    len         = 0;
            Byte   unused_bits = 0;
            UInt32 i           = 0;
            UInt32 bytes_used  = 0;

            //bitstring_init(bit_string);
            bytes_used = len_value - 1;
            for (i = 0; i < bytes_used; i++)
            {
                bit_string.set_octet((Byte)i, bit_string.byte_reverse_bits(apdu[pos + len++]));
            }
            unused_bits = (Byte)(apdu[pos] & 0x07);
            bit_string.set_bits_used((Byte)bytes_used, unused_bits);
            return(len);
        }