VC2010 bug with optimization of bit-shift operator - by hhoppe

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 720646 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 1/24/2012 8:51:43 PM
Access Restriction Public


A simple C++ program below produces the correct result in Debug but incorrect result in Release mode.
(This is true with either Win32 or x64 compiler.)

The unexpected result is:
d=0 1<<d=1
d=0 1<<d=0

#include <stdio.h>

bool gtrue=true;

void test()
    int d = -1;
    if (gtrue) d++;
    printf("d=%d 1<<d=%d\n", d, 1<<d);

void main()
    for (int j=0; j<1; j++)
Sign in to post a comment.
Posted by Microsoft on 4/29/2014 at 12:24 PM
Thank you for reporting this issue. This issue has been fixed in Visual Studio 2013. You can install a trial version of Visual Studio 2013 with the fix from:
Posted by Microsoft on 8/20/2013 at 9:42 AM
w17, thank you for your feedback. This issue is fixed and does not reproduce with either Visual Studio 2012 or the latest internal builds of Visual Studio 2013 (public Preview release available @

In the future, may I suggest that you open new issues rather than post to issues such as this one that have been closed for a significant amount of time. This will help improve your experience as responses to closed issues may go unnoticed, particular responses to issues closed as long as this one (18+ months).

ian Bearman
VC++ Code Generation and Optimization Team
Posted by w17 on 8/20/2013 at 5:57 AM
The bug is reproduced with following CL command line:
CL /nologo /FD /MD /GF /Gy /W4 /WX /O2 /Ob2 /D _MCBS /D _CONSOLE /Foshift_bug.obj -c shift_bug.cpp

If /O2 changed to /Os or /Ot bug is not reproduced.
Posted by Dmitry Me on 1/27/2012 at 2:10 AM
Disabling optimizations looks like an overkill. The problem doesn't seem to repro when optimizing "for smaller code", so optimization "for smaller code" could be used for the function alone.
Posted by Microsoft on 1/26/2012 at 5:48 PM
Thanks for reporting this. I can confirm the incorrect generated code and i can also confirm that this will be fixed in the next release of the compiler. Disabling optimizations for the function with incorrect code can be used to work around the problem.

Please let me know if you have any additional questions or concerns regarding this issue.

ian Bearman
VC++ Code Generation and Optimization Team
Posted by Dmitry Me on 1/25/2012 at 11:01 PM
I reproed this after switching from /Os to /Ot - here's the disassembly I get

291: int _tmain(int /*argc*/, _TCHAR* /*argv[]*/)
292: {
00403940 push        esi
293:     test();
00403941 mov         esi,dword ptr [__imp__printf (4050C4h)]
00403947 push        1
00403949 push        0
0040394B push        offset string "d=%d 1<<d=%d\n" (40564Ch)
00403950 call        esi
294:     for (int j=0; j<1; j++)
295:         test();
00403952 push        0
00403954 push        0
00403956 push        offset string "d=%d 1<<d=%d\n" (40564Ch)
0040395B call        esi
0040395D add         esp,18h
296:     return 0;
00403960 xor         eax,eax
00403962 pop         esi
297: }
00403963 ret

Clearly the optimizer broke the code.
Posted by MS-Moderator07 [Feedback Moderator] on 1/25/2012 at 7:26 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by hhoppe on 1/25/2012 at 9:17 AM
I reproduced the bug by building a plain C++ Console project (all parameters default) in VC2010SP1.
I just now attached a ZIP file with 3 files: sln, vcxproj, cpp.
The bug should be reproducible by selecting "Win32 - Release" and hitting control-F5. I'm running on an Intel Core i7.
Posted by Dmitry Me on 1/25/2012 at 6:51 AM
Works for me on VS 2k10 SP1. Perhaps some specific compilation options are required to reproduce this and you should specify them so that the problem can be reproduced.
Posted by MS-Moderator01 on 1/24/2012 at 9:44 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(