Mail Archives: djgpp/1998/06/30/09:30:34
On Mon, 29 Jun 1998 22:14:58 GMT, sparhawk AT eunet DOT at (Gerhard Gruber)
wrote:
>>You don't seem to understand what I mean. There is a fundamental difference
>>between arethmetic and logical shifting. Arethmetic shifting shifts the data
>>(bytes, words or longs) but keeps the sign bit intact. Logical shifting
>>treats the sign bit as any other bit in the byte/word/long.
>>So for instance, aretmetically shifting left the value -2 would give the
>>result -4; logically shifting left -2 would give 4.
>I think this is wrong. shifting left -2 always gives -4 because the bit
>pattern still has the highest bit set. in hex this would be -2 = 0xFFFE and a
>shift would result in 0xFFFC which still is -4. Problems would occure if you
>shift left a value like 0x8001 because then the bit that shows the sign is
>moved out and the result would be 0x0002 instead of 0x8002.
What "arithmetic shift" really does depends on the signed integer
model of the processor. In a sign/magnitude implementation a 8 bit
signed int with the value -2 is
10000010
A correct arithmetic shift left on this system would produce -4 alias
10000100
too, because here it would mean "shift bits #0 to #6 only". If signed
integers are implemented as 2's complement as on INTEL based machines,
a "logical left shift" and an "arithmetic left shift" are the same
operation. Arithmetic shift_left by 1 with signed integers overflows
whenever bits #7 and #6 are not identical.
Regards
Horst
- Raw text -