-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
373 - Did you know that C++23 added constexpr
bitset
?
- Loading branch information
1 parent
adba672
commit 123c8a9
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<details open><summary>Info</summary><p> | ||
|
||
* **Did you know that C++23 added constexpr `bitset`?** | ||
|
||
* https://wg21.link/P2417 | ||
|
||
</p></details><details open><summary>Example</summary><p> | ||
|
||
```cpp | ||
#include <bitset> | ||
constexpr std::bitset<8> bs{0b00000001}; | ||
static_assert(bs.test(0)); | ||
static_assert(not bs.test(1)); | ||
``` | ||
> https://godbolt.org/z/W4v1Kqcfv | ||
</p></details><details open><summary>Puzzle</summary><p> | ||
* **Can you implement bitset `reverse`?** | ||
```cpp | ||
template<auto Size> | ||
constexpr auto reverse(std::bitset<Size> bs); // TODO | ||
static_assert(0b10000000 == reverse(std::bitset<8>{0b00000001})); | ||
static_assert(0b10000001 == reverse(std::bitset<8>{0b10000001})); | ||
static_assert(0b11100001 == reverse(std::bitset<8>{0b10000111})); | ||
``` | ||
|
||
> https://godbolt.org/z/d75sW5E9x | ||
</p></details> | ||
|
||
</p></details><details><summary>Solutions</summary><p> | ||
|
||
```cpp | ||
template<auto Size> | ||
constexpr auto reverse(std::bitset<Size> bs) { | ||
std::bitset<Size> r{}; | ||
for (auto i = 0; i < bs.size(); ++i) { | ||
r[r.size()-i-1] = bs[i]; | ||
} | ||
return r; | ||
} | ||
|
||
static_assert(0b10000000 == reverse(std::bitset<8>{0b00000001})); | ||
static_assert(0b10000001 == reverse(std::bitset<8>{0b10000001})); | ||
static_assert(0b11100001 == reverse(std::bitset<8>{0b10000111})); | ||
``` | ||
> https://godbolt.org/z/8h9PGP5o6 | ||
</p></details> |