Solved

Bit shifting via Visual C++ 5.0

Posted on 1998-07-23
10
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 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