<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Right-shift operator returns incorrect and inconsistent values"
   href="http://llvm.org/bugs/show_bug.cgi?id=19500">19500</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Right-shift operator returns incorrect and inconsistent values
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.4
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>chris.cooper@veranmedical.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=12416" name="attach_12416" title="Short source code showing the problem">attachment 12416</a> <a href="attachment.cgi?id=12416&action=edit" title="Short source code showing the problem">[details]</a></span>
Short source code showing the problem

Compiling the attached test program with no flags (just "clang++ shift.cpp")
shows several problems with the right-shift operator.

Right-shifting by a large value should always return zero (according to my
reading of the C++ ISO spec, section 5.8.3).

1) Executing the program "as is" gives this output:

Main: 48271 >> 1291394886 = 754; rx=0=0x0=long:0; (lx>>ix)=long:754
Test: 48271 >> 1291394886; (lx>>ix)=754; rx=754=0x2f2=long:754;
(lx>>ix)=long:754
lx (8 bits) = 48271 =  = bc8f
ix (4 bits) = 1291394886 =  = 4cf91f46

1a) In the main() function, storing the result of the shift in a variable (rx)
results in the correct zero value, but passing the result of the shift either
to std::cout or to a simple print_value() function gives a non-zero result
(754).

1b) When the exact same values are passed to a function for evaluation, the
right-shift operator returns the same invalid value whether the result is
stored first or not.

Perhaps more disturbing:

Commenting out the last two lines of the main() function results in a
drastically wrong value being stored and reported:

Main: 48271 >> 1291394886 = 754;
rx=140734645460120=0x7fff568cdc98=long:140734645460120; (lx>>ix)=long:754</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>