Solved

Bit shifting via Visual C++ 5.0

Posted on 1998-07-23
10
201 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now