Re: Two's complement integer divided by Powers of 2 question
- From: websnarf@xxxxxxxxx
- Date: 8 Jul 2005 23:05:36 -0700
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/
.
- References:
- Prev by Date: Is this the correct?!
- Next by Date: Re: Linking a C++ library to a C program.
- Previous by thread: Re: Two's complement integer divided by Powers of 2 question
- Next by thread: Linking a C++ library to a C program.
- Index(es):
Relevant Pages
|
Loading