0

I have an array of floating point values. The array is converted to binary value array on the basis of a threshold value. e.g

A = 0.3,1.3,4.4,6.4;

if threshold = 4

A = 0, 0, 1, 1;

Now, I want to convert it to bitset. Is there an easy way to do it.

I am new to programming so sorry if the question is too silly.

Thanks

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
user3234277
  • 63
  • 1
  • 5

2 Answers2

1
float floats[16] = {/*...*/};

std::bitset<16> bits;

for(size_t i = 0; i < 16; ++i)
    bits.set(i, floats[i] < 4);
David
  • 27,652
  • 18
  • 89
  • 138
1

You can just convert the array of floats directly to a std::bitset when looping over it:

std::array<float, N> values = {...};
std::bitset<N> bitset;

for (std::size_t i = 0; i < values.size(); ++i) {
    bitset[i] = (values[i] > threshold);
}
ajshort
  • 3,684
  • 5
  • 29
  • 43