delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/12/12:03:32

Message-Id: <3.0.6.32.20000512104849.0079f560@pop.crosswinds.net>
X-Sender: shinelight AT pop DOT crosswinds DOT net
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Fri, 12 May 2000 10:48:49 -0500
To: djgpp AT delorie DOT com
From: "Thomas J. Hruska" <shinelight AT crosswinds DOT net>
Subject: Re: Right shift
In-Reply-To: <391c1891.23890473@news.freeserve.net>
References: <391C0409 DOT 79567772 AT tiscalinet DOT it>
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

At 02:43 PM 5/12/00 GMT, you wrote:
>night DOT walker AT tiscalinet DOT it wrote:
>
>>Why right-shifting ">>" a negative signed int will first turn it
>>positive?
>>
>>4 >>1 = 2 but also -4 >> 1 = 2!!
>
>I don't see how you get 2.  I get -2.
>
>But you shouldn't right-shift negative numbers anyway if you want your
>code to be portable, because the results are implementation-defined.
>
>>What EXACTLY happens?
>
>In the case of DJGPP, the compiler uses the SAR instruction.  This fills
>the vacated bits with copies of the sign bit.
>
>S.

To solve the problem, you will want to either cast the variable type to
unsigned before performing the shift or just start with the variable as
unsigned in the first place.


           Thomas J. Hruska -- shinelight AT crosswinds DOT net
Shining Light Productions -- "Meeting the needs of fellow programmers"
                  http://www.shininglightpro.com/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019