{"id":6042,"date":"2020-12-03T16:36:37","date_gmt":"2020-12-03T16:36:37","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/bit-manipulation-with-c-20\/"},"modified":"2023-06-26T09:36:28","modified_gmt":"2023-06-26T09:36:28","slug":"bit-manipulation-with-c-20","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/bit-manipulation-with-c-20\/","title":{"rendered":"Bit Manipulation with C++20"},"content":{"rendered":"<p>This post concludes my presentation of library features in C++20. Today I am writing about the class<code> std::source_location<\/code> and a few functions for bit manipulation.<\/p>\n<p><!--more--><\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6037\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/TimelineCpp20CoreLanguage2.png\" alt=\"TimelineCpp20CoreLanguage2\" width=\"650\" height=\"265\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/TimelineCpp20CoreLanguage2.png 966w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/TimelineCpp20CoreLanguage2-300x122.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/TimelineCpp20CoreLanguage2-768x313.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><code>std::source_location<\/code><\/h2>\n<p><code>std::source_location<\/code> represents information about the source code. This information includes file names, line numbers, and function names. The information is precious when you need information about the call site for debugging, logging, or testing purposes. The class <code>std::source_location<\/code> is the better alternative for the predefined C++11 macros <code>__FILE__<\/code> and<code> __LINE__<\/code> and should, therefore, be used.<\/p>\n<p>The following table shows the interface of <code>std::source_location<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6038\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/sourceLocation.png\" alt=\"sourceLocation\" width=\"500\" height=\"161\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/sourceLocation.png 1162w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/sourceLocation-300x96.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/sourceLocation-1024x329.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/sourceLocation-768x247.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The call <code>std::source_location::current()<\/code> creates a new source location object<code> src. sr<\/code>c represents the information of the call site. Now, no C++ compiler supports <code>std::source_location<\/code>. Consequently, the following program <code>sourceLocation.cpp<\/code> is from <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/source_location\">cppreference.com\/source_location<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ sourceLocation.cpp<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ from cppreference.com<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string_view&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;source_location&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">log<\/span>(std<span style=\"color: #555555;\">::<\/span>string_view message,\r\n         <span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>source_location<span style=\"color: #555555;\">&amp;<\/span> location <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>source_location<span style=\"color: #555555;\">::<\/span>current())\r\n{\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"info:\"<\/span>\r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> location.file_name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">':'<\/span>\r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> location.line() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">' '<\/span>\r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> message <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>()\r\n{\r\n    log(<span style=\"color: #cc3300;\">\"Hello world!\"<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ info:main.cpp:19 Hello world!<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<div>&nbsp;<\/div>\n<p>The output of the program is part of its source code.<\/p>\n<p>C++20 makes it quite comfortable to access or manipulate bits or bit sequences.<\/p>\n<\/p>\n<h2>Bit Manipulation<\/h2>\n<p>Thanks to the new type std::endian, you get the endianness of a scalar type.<\/p>\n<h3>Endianness<\/h3>\n<ul>\n<li>Endianness can be big-endian or little-endian. Big-endian means the most significant byte comes first; little-endian means that the least significant byte comes first.<\/li>\n<li>A scalar type is either an arithmetic type, an <code>enum<\/code>, a pointer, a member pointer, or a <code>std::nullptr_t<\/code>.<\/li>\n<\/ul>\n<p>The class <code>endian<\/code> provides the endianness of all scalar types:<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">enum<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">endian<\/span>\r\n{\r\n    little <span style=\"color: #555555;\">=<\/span> <span style=\"color: #0099ff; font-style: italic;\">\/*implementation-defined*\/<\/span>,\r\n    big    <span style=\"color: #555555;\">=<\/span> <span style=\"color: #0099ff; font-style: italic;\">\/*implementation-defined*\/<\/span>,\r\n    native <span style=\"color: #555555;\">=<\/span> <span style=\"color: #0099ff; font-style: italic;\">\/*implementation-defined*\/<\/span>\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>If all scalar types are little-endian, <code>std::endian::native<\/code> is equal to <code>std::endian::little<\/code>.<\/li>\n<li>If all scalar types are big-endian,<code> std::endian::native<\/code> is equal <code>to std::endian::big<\/code>.<\/li>\n<\/ul>\n<p>Even corner cases are supported:<\/p>\n<ul>\n<li>If all scalar types have <code>sizeof<\/code> 1 and therefore endianness does not matter; the values of the enumerators <code>std::endian::little<\/code>, <code>std::endian::big<\/code>, and <code>std::endian::native<\/code> are identical.<\/li>\n<li>If the platform uses mixed endianness, <code>std::endian::native<\/code> is neither equal to <code>std::endian::big<\/code> nor <code>std::endian::little<\/code>.<\/li>\n<\/ul>\n<p>When I perform the following program <code>getEndianness.cpp<\/code> on an x86 architecture, I get the answer little-endian.<\/p>\n<div>&nbsp;<\/div>\n<div>\n<div>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ getEndianness.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;bit&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (std<span style=\"color: #555555;\">::<\/span>endian<span style=\"color: #555555;\">::<\/span>native <span style=\"color: #555555;\">==<\/span> std<span style=\"color: #555555;\">::<\/span>endian<span style=\"color: #555555;\">::<\/span>big) {\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"big-endian\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (std<span style=\"color: #555555;\">::<\/span>endian<span style=\"color: #555555;\">::<\/span>native <span style=\"color: #555555;\">==<\/span> std<span style=\"color: #555555;\">::<\/span>endian<span style=\"color: #555555;\">::<\/span>little) {\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"little-endian\"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ little-endian<\/span>\r\n    }\r\n\r\n}\r\n<\/pre>\n<\/div>\n<div>&nbsp;<\/div>\n<div><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/if\"><code>constexpr if<\/code><\/a> enables it to compile source code conditionally. This means that the compilation depends on the endianness of your architecture. If you want to know more about endianness, read the same-named <a href=\"https:\/\/en.wikipedia.org\/wiki\/Endianness\">Wikipedia page<\/a>.<\/div>\n<\/div>\n<\/div>\n<h3>Accessing or Manipulating Bits or Bit Sequences<\/h3>\n<p>The following table gives you the first overview of all functions.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6039\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitInterface5.png\" alt=\"bitInterface5\" width=\"600\" height=\"222\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitInterface5.png 1171w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitInterface5-300x111.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitInterface5-1024x380.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitInterface5-768x285.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The functions except <code>std::bit_cast<\/code> require an unsigned integer type (<code>unsigned char, unsigned short, unsigned int, unsigned long,<\/code> or<code> unsigned long long<\/code>).<\/p>\n<p>The program<code> bit.cpp<\/code> shows the usage of the functions.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ bit.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;bit&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;bitset&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">uint8_t<\/span> num<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>b00110010;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::has_single_bit(0b00110010): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>has_single_bit(num) \r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::bit_ceil(0b00110010): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(std<span style=\"color: #555555;\">::<\/span>bit_ceil(num)) \r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::bit_floor(0b00110010): \"<\/span> \r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(std<span style=\"color: #555555;\">::<\/span>bit_floor(num)) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::bit_width(5u): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bit_width(<span style=\"color: #ff6600;\">5u<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::rotl(0b00110010, 2): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(std<span style=\"color: #555555;\">::<\/span>rotl(num, <span style=\"color: #ff6600;\">2<\/span>)) \r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::rotr(0b00110010, 2): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(std<span style=\"color: #555555;\">::<\/span>rotr(num, <span style=\"color: #ff6600;\">2<\/span>)) \r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::countl_zero(0b00110010): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>countl_zero(num) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::countl_one(0b00110010): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>countl_one(num) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::countr_zero(0b00110010): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>countr_zero(num) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::countr_one(0b00110010): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>countr_one(num) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::popcount(0b00110010): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>popcount(num) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Here is the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6040\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bit2.png\" alt=\"bit2\" width=\"411\" height=\"286\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bit2.png 411w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bit2-300x209.png 300w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/p>\n<p>The next program shows the application and the output of the functions&nbsp;<code>std::bit_floor<\/code>,<code> std::bit_ceil<\/code>, <code>std::bit_width<\/code>, and <code>std::bit_popcount<\/code> for the numbers 2 to 7.&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ bitFloorCeil.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;bit&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;bitset&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2u<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">8u<\/span>; <span style=\"color: #555555;\">++<\/span>i) {\r\n         std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"bit_floor(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") = \"<\/span> \r\n                   <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bit_floor(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"bit_ceil(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") = \"<\/span> \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bit_ceil(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"bit_width(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") = \"<\/span> \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bit_width(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n                  \r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"bit_popcount(\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bitset<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">8<\/span><span style=\"color: #555555;\">&gt;<\/span>(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") = \"<\/span> \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>popcount(i) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;   \r\n        \r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6041\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitFloorCeil.PNG\" alt=\"bitFloorCeil\" width=\"250\" height=\"644\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitFloorCeil.PNG 291w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/12\/bitFloorCeil-116x300.png 116w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Additionally to coroutines, C++20 has much to offer for concurrency. First, C++20 has new atomics. The new atomics exist for floating-point values and smart pointers. C++20 also enables waiting on atomics. To coordinate threads, semaphores, latches, and barriers come into play. Also, the <code>std::thread<\/code> was improved with <code>std::jthread<\/code>. The execution of a&nbsp;<code>std::jthread <\/code>can be interrupted and joins automatically in its destructor.<code><br \/><\/code><\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<div id=\"simple-translate\">&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This post concludes my presentation of library features in C++20. Today I am writing about the class std::source_location and a few functions for bit manipulation.<\/p>\n","protected":false},"author":21,"featured_media":6037,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[452],"class_list":["post-6042","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-bit-manipulation"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6042"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6042\/revisions"}],"predecessor-version":[{"id":6721,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6042\/revisions\/6721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6037"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}