Better int conversion

pull/29182/head
Craig Tiller 3 years ago
parent d135c61043
commit 4001b957cb
  1. 8
      src/core/lib/compression/compression_internal.cc
  2. 12
      src/core/lib/gprpp/bitset.h

@ -194,13 +194,7 @@ CompressionAlgorithmSet CompressionAlgorithmSet::FromString(
}
uint32_t CompressionAlgorithmSet::ToLegacyBitmask() const {
uint32_t x = 0;
for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
if (set_.is_set(i)) {
x |= (1u << i);
}
}
return x;
return set_.ToInt<uint32_t>();
}
absl::optional<grpc_compression_algorithm>

@ -153,6 +153,18 @@ class BitSet {
return true;
}
template <typename Int>
typename std::enable_if<std::is_unsigned<Int>::value &&
(sizeof(Int) * 8 >= kTotalBits),
Int>::type
ToInt() const {
Int result = 0;
for (size_t i = 0; i < kTotalBits; i++) {
if (is_set(i)) result |= (Int(1) << i);
}
return result;
}
private:
// Given a bit index, return which unit it's stored in.
static constexpr size_t unit_for(size_t bit) { return bit / kUnitBits; }

Loading…
Cancel
Save