www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/30/17:16:50

Sender: nate AT cartsys DOT com
Message-ID: <359954C9.3B354F0@cartsys.com>
Date: Tue, 30 Jun 1998 14:12:41 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
MIME-Version: 1.0
To: Arthur <arfa AT clara DOT net>
CC: DJGPP Mailing List <djgpp AT delorie DOT com>
Subject: Re: 64k demo
References: <00ea01bda399$4f778a80$364e08c3 AT arthur>

Arthur wrote:
> 
> >No! if you apply >> and << to an unsigned you get logical stuff. The
> arithmetic
> >is used only for signeds.
> 
> Ahh, I see. Can you not do it "manually", though? 

You could probably do some fancy casting to do an arithmetic shift of an
unsigned number, or vice versa.  Something like:

unsigned u;
*((int *)&u) <<= 2;

> Also, can you do a ROL/ROR
> in C or do you have to use inline ASM? And what about "shift with carry?"

There's no builtin rotate operator in C.  You can, however, simulate it
with some shifts:

/* rotate n right by b bits, assuming a 32-bit word */

n = (n >> b) | (n << (32 - b));

For the case of `b' == constant, GCC on an x86 will actually optimize
this into:

rorl $b, n  /* !!! */

Amazing.

You can also simulate a shift with carry; this is left as an exercise
for the reader.  ;-)
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


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