Boost C++ Libraries: Ticket #11999: int128_t from cpp_int gives unexpected results when bitshifting https://svn.boost.org/trac10/ticket/11999 <p> The 128-bit signed integer int128_t from cpp_int gives some unexpected results when using the bitshift operator. If I shift the value -1 left by 31 bits, then shift it back right by 32 bits, I get the value -0 instead of -1. I do not encounter this problem with the signed integers of larger size (256, 512, 1024). The code below illustrates the problem. I'm using g++ 5.3.0 on Linux Mint (I can test it with Visual Studio 2015 on Windows 7 later if desired). </p> <p> To be clear: this only seems to happen when shifting right further than left. If I shift right by 31 they all print -1. It's also not just a bug in the conversion to string: if I multiply by 10, int128_t prints 0, but the rest prints -10. </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;cstdint&gt; #include &lt;boost/multiprecision/cpp_int.hpp&gt; using namespace boost::multiprecision; int main() { int64_t i0 = -1; i0 &lt;&lt;= 31; i0 &gt;&gt;= 32; std::cout &lt;&lt; i0 &lt;&lt; std::endl; // prints -1 int128_t i1 = -1; i1 &lt;&lt;= 31; i1 &gt;&gt;= 32; std::cout &lt;&lt; i1 &lt;&lt; std::endl; // prints -0 int256_t i2 = -1; i2 &lt;&lt;= 31; i2 &gt;&gt;= 32; std::cout &lt;&lt; i2 &lt;&lt; std::endl; // prints -1 int512_t i3 = -1; i3 &lt;&lt;= 31; i3 &gt;&gt;= 32; std::cout &lt;&lt; i3 &lt;&lt; std::endl; // prints -1 int1024_t i4 = -1; i4 &lt;&lt;= 31; i4 &gt;&gt;= 32; std::cout &lt;&lt; i4 &lt;&lt; std::endl; // prints -1 return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11999 Trac 1.4.3 John Maddock Fri, 19 Feb 2016 19:32:27 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11999#comment:1 https://svn.boost.org/trac10/ticket/11999#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in <a class="ext-link" href="https://github.com/boostorg/multiprecision/commit/2f635b45ffc0517a67e8210239a587a29b62575a"><span class="icon">​</span>https://github.com/boostorg/multiprecision/commit/2f635b45ffc0517a67e8210239a587a29b62575a</a> </p> <p> Note however that bit-shifting a signed integer invokes undefined behaviour, so the std::int64_t behaviour above is at the whim of the specific machine/compiler. </p> Ticket