delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1998/11/17/10:20:18

X-Authentication-Warning: acp3bf.physik.rwth-aachen.de: broeker owned process doing -bs
Date: Tue, 17 Nov 1998 16:06:16 +0100 (MET)
From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
X-Sender: broeker AT acp3bf
To: DJGPP Workers List <djgpp-workers AT delorie DOT com>
Subject: Re: gcc: feature or bug?
In-Reply-To: <Pine.LNX.3.93.981117142107.7061M-100000@acp3bf>
Message-ID: <Pine.LNX.3.93.981117155212.7061Q-100000@acp3bf>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

On Tue, 17 Nov 1998, Hans-Bernhard Broeker wrote:

> > Let's say I have a 32-bit unsigned integer, and I want to clear it's
> > most significant bit. I wrote this code:
> > 
> > 	(x*2)/2
> > 
> > And of course(?) it doesn't work. Is it a bug in gcc or a bug in me?

> 
> A bug in you :-) To get what you want, you should have written either
[...]

OOOPS. Seems I've misinterpreted what was wanted. My code examples are
for clearing the *least* significant bits, of course :-(

Anyway: I suspect the compiler actually is allowed to do this. The basic
problem is that unsigned arithmetic is defined (by the ANSI C Standard) 
to work modulo 2^n, where n is the number of bits in an unsigned int (32,
for DJGPP), but the result of division by 2 is *not* unambiguosly defined
in these circumstances, as there are two numbers that fulfill 

	a*2 = b  (modulo 2^n)

for every given even number b. Only one of them is the result you'ld
expect (the conventional b/2), the other is b/2+2^(n-1).

Whatever the real answer might be: this question is really not on-topic in
here. It should be carried over to the responsible newsgroup,
comp.lang.c.std. That's where the gurus with their copies of the C
standard are to be found. 


Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


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