Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


C - logical vs arithmetic right shift


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 StubornAH   Members   -  Reputation: 138

Like
0Likes
Like

Posted 27 September 2005 - 06:24 AM

I have an assignment to do a logical shift n spots to the right. On my WinXP machine using Dev-C++ >> is a logical shift. On my school's Linux machine, or maybe there is a flag in the compiler?, it does an arithmetic shift. C is not taught in this class, just general programming. I have search dozens of C websites and they all just tell me that >> does a right shift. How do I tell C I want a logical shift or an arithmetic shift? Thanks.

Sponsor:

#2 Tinyn   Members   -  Reputation: 193

Like
0Likes
Like

Posted 27 September 2005 - 06:26 AM

>>>

#3 doynax   Members   -  Reputation: 850

Like
0Likes
Like

Posted 27 September 2005 - 06:27 AM

In general C compilers perform logical right shifts on unsigned data types and arithmetic right shifts (those that maintain the sign) on signed data types.
However it's something of a trick question since the compiler isn't required to support arithmetic ones at all and may choose to use logical ones exclusively.

It's also interesting to know that a division by two is not necessarily (and almost always in practice) equivalent to a two's complement arithmetic right shift. The division rounds towards zero (although it doesn't have to) while the shift does not.
signed a = -10;
unsigned b = a;
a >>= 1; // aritmetic (probably)
b >>= 1; // logical

Quote:
Original post by Tinyn
>>>

That's actually a Java operator.


BTW does anyone know what the standard says about one's completement machines and arithmetic right shifts? You'd think that the natural implementation, unlike the two's complement version, would round towards zero instead.

#4 Tinyn   Members   -  Reputation: 193

Like
0Likes
Like

Posted 27 September 2005 - 06:30 AM

Huh, I coulda sworn it was in C++ too.

#5 MaulingMonkey   Members   -  Reputation: 1556

Like
0Likes
Like

Posted 27 September 2005 - 06:33 AM

Quote:
Original post by Tinyn
Huh, I coulda sworn it was in C++ too.


Nope. MSDN Operator List

#6 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 27 September 2005 - 06:59 AM

You could also use some inline asm. iirc, shl sal shr sar

#7 ZQJ   Members   -  Reputation: 496

Like
0Likes
Like

Posted 27 September 2005 - 07:33 AM

Do any modern CPUs actually use one's complement?

#8 doynax   Members   -  Reputation: 850

Like
0Likes
Like

Posted 27 September 2005 - 07:39 AM

Quote:
Original post by ZQJ
Do any modern CPUs actually use one's complement?
None that I know of (aside IEEE floats which kind of uses it internally). They do introduce some interesting exceptions in the language however. So they're great when you want to say 'well.. that's technically incorrect' =)

I wouldn't be surprised if a few old beasts were still up and running (even actively developed for) though..




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS