[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Bit shifting via Visual C++ 5.0

Posted on 1998-07-23
10
Medium Priority
?
210 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 150 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

656 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