[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bit shifting via Visual C++ 5.0

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

864 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