asn1_primitives_setters.h
Go to the documentation of this file.
1 /*
2 =============================================================================
3 MIT License
4 
5 Copyright (c) 2023-2025 Institute for Automotive Engineering (ika), RWTH Aachen University
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 =============================================================================
25 */
26 
27 #ifndef ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_ASN1_PRIMITIVES_SETTERS_H
28 #define ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_ASN1_PRIMITIVES_SETTERS_H
29 
42 template <typename T>
43 inline void setBitString(T& bitstring, const std::vector<bool>& bits) {
44  // bit string size
45  const int bits_per_byte = 8;
46  const int n_bytes = (bits.size() - 1) / bits_per_byte + 1;
47  const int n_bits = n_bytes * bits_per_byte;
48 
49  // init output
50  bitstring.bits_unused = n_bits - bits.size();
51  bitstring.value = std::vector<uint8_t>(n_bytes);
52 
53  // loop over all bytes in reverse order
54  for (int byte_idx = n_bytes - 1; byte_idx >= 0; byte_idx--) {
55 
56  // loop over bits in a byte
57  for (int bit_idx_in_byte = bits_per_byte - 1; bit_idx_in_byte >= 0; bit_idx_in_byte--) {
58 
59  // map bit index in byte to bit index in total bitstring
60  int bit_idx = (n_bytes - byte_idx - 1) * bits_per_byte + bit_idx_in_byte;
61  if (byte_idx == 0 && bit_idx < bitstring.bits_unused) break;
62 
63  // set bit in output bitstring appropriately
64  if (bit_idx < bits.size()) {
65  bitstring.value[byte_idx] |= bits[bits_per_byte - bit_idx - 1] << bit_idx_in_byte;
66  }
67  }
68  }
69 }
70 
71 #endif // ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_SETTERS_H
setBitString
void setBitString(T &bitstring, const std::vector< bool > &bits)
Set a Bit String by a vector of bools.
Definition: asn1_primitives_setters.h:43


etsi_its_msgs_utils
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:32:12