Re: Two's complement integer divided by Powers of 2 question



Janice wrote:
> I got this question from my textbook and I cannot understand the theory.
> When a signed positive integer X divided by pow(2,k), the result is shifting
> k bits to right and putting w-k bits of 0 from the most significant bits.
> However, when the X is a negative number divided by pow(2,k), the shifting
> and sign bit extension method doesnt give the correct answer?
> What kind of bias should we make on the X before the division?

This is a bit involved.

1) Floating point rounds towards zero, whereas right shifting usually
rounds towards INT_MIN. (Using pow(2,k) forces promotion to floating
point.)

2) The ANSI C standard does not dictate anything useful about how right
shift works. Basically some old obsolete hardware that the ANSI C
standard committee insists on continuing to coddle cannot perform
correct right shifting. Rather than forcing that very marginal old
hardware to emulate the correct behavior (much as they forced the most
popular microprocessor family of all time, and widely deployed
desktop/workstation processor in history to emulate FP before they
could manage to include on-chip support for it) for this less commonly
used operation, the standard just decided not to define a single
behavior for it.

So whether or not some calculation is equal or consistant with right
shift is not up to the numerical or mathematical properties of ordinary
calculations. Its actually up to the capriciousness of the compiler
implementor. (Though its not surprising that your textbook thinks this
behavior deterministic, since 99.9999999% of all platforms do perform
correctly.)

--
Paul Hsieh
http://www.pobox.com/~qed/

.



Relevant Pages

  • Re: Two questions about multiple personalities.
    ... situations that have arisen in my Unpublished* Masterpiece. ... they don't occur often enough for a "standard ... shifting names to indicate "public" actions done by cover identities vs. ...
    (rec.arts.sf.composition)
  • Re: How small can you go? (granny gear w/ STI)
    ... >> I've heard many people suggest replacing the 30 on a standard ... >> before STI shifting just doesn't work anymore? ... the chain is more likely to miss it when shifting from the ... A smaller middle might smooth things out a bit. ...
    (rec.bicycles.tech)
  • Re: help w/ c/c++ problem
    ... thinking the envp parameter is standard as we get thinking ... 'void main' is standard. ... It may be a nearly-C textbook. ...
    (comp.lang.c)
  • Re: R6RS Progress Report
    ... standard to tell it what to do, but a portable Scheme program ... It may be worth noting that textbook authors also need a standard to ... isn't the intent of the editors to abolish or impoverish the top level. ...
    (comp.lang.scheme)
  • Re: arm9e: how do I invert sign of a halfword?
    ... The point I was raising was that shifting a value between ... Why don't you read the standard once in a while? ... but the C language does. ... (C99, txt) ...
    (comp.arch.embedded)

Loading