<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3656: Inconsistent bit operations returning a count</title>
<meta property="og:title" content="Issue 3656: Inconsistent bit operations returning a count">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3656.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++23">C++23</a> status.</em></p>
<h3 id="3656"><a href="lwg-defects.html#3656">3656</a>. Inconsistent bit operations returning a count</h3>
<p><b>Section:</b> 22.11.5 <a href="https://wg21.link/bit.pow.two">[bit.pow.two]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Nicolai Josuttis <b>Opened:</b> 2021-12-30 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Among the bit operations returning a count, <code>bit_width()</code> is the only one not returning an <code>int</code>.
<p/>
This has the following consequences:
</p>
<blockquote><pre>
std::uint64_t b64 = 1;
b64 = std::rotr(b64, 1);
int count1 = std::popcount(b64);     // OK
int count2 = std::countl_zero(b64);  // OK
int count3 = std::bit_width(b64);    // OOPS
</pre></blockquote>
<p>
The last line may result in a warning such as:
</p>
<blockquote><p>
Warning: conversion from <code>long long unsigned</code> to <code>int</code> may change value
</p></blockquote>
<p>
You have to use a <code>static_cast</code> to avoid the warning.
<p/>
Note that <a href="https://wg21.link/P0553R4" title=" Bit operations">P0553R4</a> explicitly states the following design considerations,
which I would also assume to apply to the later added functions from <a href="https://wg21.link/P0556R3" title=" Integral power-of-2 operations">P0556R3</a>:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<p>
The counting operations return "<code>int</code>" quantities, consistent with the rule "use an <code>int</code> unless you 
need something else". This choice does not reflect, in the type, the fact that counts are always non-negative. 
</p>
</blockquote>

<p><i>[2022-01-30; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
Eight votes for P0, but request LEWG confirmation before setting it to
Tentatively Ready.
</p>

<p><i>[2022-02-22 LEWG telecon; Status changed: LEWG &rarr; Open]</i></p>

<p>
No objection to unanimous consent to send the proposed resolution for LWG3656 to LWG for C++23.
The changes in P1956 changed the functions to be more counting than mathematical.
</p>


<p><i>[2022-07-08; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after ten votes in favour during reflector poll.
</p>

<p><i>[2022-07-15; LWG telecon: move to Ready]</i></p>


<p><i>[2022-07-25 Approved at July 2022 virtual plenary. Status changed: Ready &rarr; WP.]</i></p>



<p id="res-3656"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4901" title=" Working Draft, Standard for Programming Language C++">N4901</a>. 
</p>

<ol>
<li><p>Modify 22.11.2 <a href="https://wg21.link/bit.syn">[bit.syn]</a>, header <code>&lt;bit&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
[&hellip;]
template&lt;class T&gt;
  constexpr <del>T</del><ins>int</ins> bit_width(T x) noexcept;
[&hellip;]
</pre>
</blockquote>
</li>

<li><p>Modify 22.11.5 <a href="https://wg21.link/bit.pow.two">[bit.pow.two]</a>, as indicated:</p>

<blockquote>
<pre>
template&lt;class T&gt;
  constexpr <del>T</del><ins>int</ins> bit_width(T x) noexcept;
</pre>
<blockquote>
<p>
-11- <i>Constraints:</i> <code>T</code> is an unsigned integer type (6.9.2 <a href="https://wg21.link/basic.fundamental">[basic.fundamental]</a>).
<p/>
-12- <i>Returns:</i> If <code>x == 0</code>, <code>0</code>; otherwise one plus the base-2 logarithm of <code>x</code>, 
with any fractional part discarded.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
