Skip to content

Commit

Permalink
[300] - solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak committed Oct 24, 2022
1 parent 54b7843 commit 43c4dc3
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion tips/300.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,65 @@ template<std::size_t ix>
}
```
> https://godbolt.org/z/W9Y6zbbMT
> https://godbolt.org/z/W9Y6zbbMT
```cpp
[[nodiscard]] consteval auto to_string(auto bitset){
return bitset.to_string();
}
```

> https://godbolt.org/z/odjMYz9hd
```cpp
template <std::size_t N>
[[nodiscard]] consteval auto to_string(std::bitset<N> bitset) {
return [&]<std::size_t... Is>(std::index_sequence<Is...>) -> std::string {
return {(bitset.test(N - Is - 1uz) ? '1' : '0')...};
}(std::make_index_sequence<N>{});
}
```
> https://godbolt.org/z/61bMGbYnh
```cpp
constexpr auto bit_to_char(auto bitset, std::size_t idx)
{
if(bitset.test(idx))
return '1';
else
return '0';
}
template<size_t N>
[[nodiscard]] constexpr auto to_string(std::bitset<N> bitset){
return [&]<size_t... I>(const std::bitset<N>& bits,std::index_sequence<I...>)->std::string{
return{(bit_to_char(bits,N-I-1))...};
}(bitset,std::make_index_sequence<N>{});
}
```

> https://godbolt.org/z/h5WhYPoaE
```cpp
template <std::size_t B>
[[nodiscard]] consteval auto to_string(std::bitset<B> bitset){
return [&]<size_t... N>(std::index_sequence<N...>) -> std::string {
constexpr auto bit_value = [](auto bit) -> char { return bit + '0';};
return {(bit_value(bitset[B-N-1]))...};
}(std::make_index_sequence<B>());
}
```
> https://godbolt.org/z/G4q61zGbx
```cpp
[[nodiscard]] consteval auto to_string(auto bitset) {
return [&]<typename T, T...ints>(std::integer_sequence<T, ints...>) {
char a[] = { (bitset[sizeof...(ints) - ints -1] ? '1' : '0') ... };
return std::string(a, sizeof...(ints));
}(std::make_index_sequence<bitset.size()>{});
}
```

> https://godbolt.org/z/EMW4rYnKP

0 comments on commit 43c4dc3

Please sign in to comment.