Mail Archives: djgpp/1998/06/30/17:16:50
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 -