Mail Archives: djgpp/1998/06/29/19:16:48
From: | sparhawk AT eunet DOT at (Gerhard Gruber)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: 64k demo
|
Date: | Mon, 29 Jun 1998 22:14:58 GMT
|
Organization: | Customer of EUnet Austria
|
Lines: | 67
|
Message-ID: | <35ac0bc8.13244573@news.Austria.EU.net>
|
References: | <008b01bda38e$369d3d80$364e08c3 AT arthur>
|
NNTP-Posting-Host: | e154.dynamic.vienna.at.eu.net
|
Mime-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Destination: "Arthur" <arfa AT clara DOT net>
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Mon, 29 Jun 1998 19:45:44 +0100:
>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.
>The shift commands which change different bits that I was referring to are
>commands like "shift with carry" and "shift with overflow."
>AFAIK, you can only do arethmetic shifting in C, like I said.
I wrote a small program that shows this.
#include <stdio.h>
int main(void)
{
long i1 = 0x80000002;
printf("\n\n\n\n\n\ni1: %u %d %04X\n", i1, i1, i1);
i1 <<= 1;
printf("i1: %u %d %04X\n", i1, i1, i1);
i1 = -2;
printf("i1: %u %d %04X\n", i1, i1, i1);
i1 <<= 1;
printf("i1: %u %d %04X\n", i1, i1, i1);
return(0);
}
and the output of this is:
i1: 2147483650 -2147483646 80000002
i1: 4 4 0004 <- this is were it goes wrong because the
middle column should read -2147483645 instead of 4.
i1: 4294967294 -2 FFFFFFFE
i1: 4294967292 -4 FFFFFFFC
--
Bye,
Gerhard
email: sparhawk AT eunet DOT at
g DOT gruber AT sis DOT co DOT at
Spelling corrections are appreciated.
- Raw text -