Solved

Bit shifting via Visual C++ 5.0

Posted on 1998-07-23
10
204 Views
Last Modified: 2010-04-02
Can anyone explain the reason behind the situation below :

the following code :

#include <stdio.h>

void main()
{
     unsigned int x,y;

     x = 0xAAAAAAAA;

    y = x >> 32;

   printf("%X",y)
}  



Produces:

      AAAAAAAA


Why is it that if I shift a 32 bit variable by 32 bits, I get the same number.  If I shift a number, say :

   y = 0xFFFFFFFF >> 32

then y=0.  ????

if I let x=FFFFFFFF and then do:

  y = X >> 31;
  y = y >> 1;

THEN I get y=0, but not if I do all the shifting at once.

Any help??????
0
Comment
Question by:huya
  • 5
  • 4
10 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1168489
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168490
The behavior of the shift operators with shift counts of or greater than the integral value's size is  undefined  (implementation defined).  In the x86 processor family (intel) this wiould be handled with the SAR instruction.  The behavior of the SAR instruction with counts that are of or larger than the operand size changed with the 286 model.  On 286's and later the SAR shifts by the count modulo 32.  Thus shifting by 33 is the same as shifting by 1.  However this is implimentation defined.  If you were to try this on a non-intel computer (or a different compiler) you might get different results.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168491
I've been looking though my manuals for the C++ rules in this area.  What I found so far is that the behavior of the shift operators is undefined if the shift is larger than the operand size, if the shift count is less than zero, or if the operand is less than zero.

I hope this helps.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Expert Comment

by:duneram
ID: 1168492
hey what happened to my post?  I said the same thing but my entry is totally gone.
0
 
LVL 2

Expert Comment

by:duneram
ID: 1168493
I answered this about an  hour ago
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168494
Are you sure it actually went through?  Like did you see your answer in the page after you pressed submit?  If EE is too bussy you might get a message to that effect and your answer might not get recorded.
0
 
LVL 2

Expert Comment

by:duneram
ID: 1168495
Yeah i know it went thru cause I saw it after I pressed the submit, and then I got an email saying someone had added a comment.  It never told me that someone had posted an answer though.  I wrote a cool little program that shows this effect and then put the output and the same 'notice' you found as the answer.  Don't worry about it.  I figure it is probably a concurrent proccessing problem with EE.  I once got a free week at the Tropicana in Las Vegas for the same thing.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168496
Lifes little things tend to balance out.  But a free week-long vacation and 50 pts?  It think you are coming out ahead.
0
 
LVL 2

Expert Comment

by:duneram
ID: 1168497
Yeah i know it went thru cause I saw it after I pressed the submit, and then I got an email saying someone had added a comment.  It never told me that someone had posted an answer though.  I wrote a cool little program that shows this effect and then put the output and the same 'notice' you found as the answer.  Don't worry about it.  I figure it is probably a concurrent proccessing problem with EE.  I once got a free week at the Tropicana in Las Vegas for the same thing.  
0
 

Author Comment

by:huya
ID: 1168498
Thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question