Lawrence Kirby wrote:
> On Thu, 16 Jun 2005 07:15:58 +0000, Richard Bos wrote:
>
>
>>CBFalconer <> wrote:
>>
>>
>>>Mehta Shailendrakumar wrote:
>>>
>>>>Can anyone suggest me operator to perform arithmetic shift in C?
>>>>May it be for a perticular compiler.
>>>
>>>There is a lot of misinformation in the replies you have received.
>>>Shift operations in C are only portable, and properly defined, on
>>>unsigned objects.
>>
>>This is not true. They are only portable, and properly defined, on
>>non-negative values. C never shifts objects, it shifts values;
>
>
> That's tricky, a shift works at the representation level, and in addition
> the standard defines the effect on values in some circumstances.
Disagreeing with L.K. in this forum carries more than a
little chance of making oneself ridiculous, but "fools rush
in" ... Here I go; draw your own conclusions:
The Standard describes the shift operators in terms of
a binary representation, but I think R.B. is right: they
operate on values, not on "the" representations. Consider
an `int' representation with padding bits: Assuming a shift
whose result is well-defined, the padding bits do not
affect any value bits of that result.
Here's a hypothetical `int' with one sign bit, fifteen
value bits, and two padding bits:
P S V V V V V V V P V V V V V V V V
1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
(I intend this to mean that the value of the `int' is 256,
that the leftmost padding bit is set, and that the padding
bit in the middle is clear.) Right-shifting this by one
position gives
P S V V V V V V V P V V V V V V V V
x 0 0 0 0 0 0 0 0 x 1 0 0 0 0 0 0 0
(Meaning: the value is 128, and the two padding bits have
unknown settings.)
Observe that the leftmost padding bit does not shift
into the sign position, nor does the middle padding bit
shift into the 128's position, nor does the 256's bit shift
into the middle padding position and get "swallowed" there.
The shift has affected the value bits in a way that is not
influenced by either of the padding bits. That is, the shift
has operated on the value, not on the representation.
The value/representation question can also be argued
without introducing exotica like padding bits. How are "bit
positions" defined in the first place? Not by reference to a
physical ordering: they could be left-to-right, top-to-bottom,
scattered in some strange pattern that conserves silicon --
they could even have no individual physical existence at all
in a computer built of four-state components each representing
a pair of bits. Nor can they be ordered by their addresses,
since even on a bit-addressable machine C has no way to talk
about the address of anything smaller than a complete `char'.
No, "bit positions" (and the "left" and "right" directions)
can only be described in terms of the powers of two in the
value.
I think the Standard's use of "representation" in describing
the shift operators is just for convenience of exposition, and
does not refer to the "representation" as described in 6.2.6.
Note that in addition to describing the shift result in terms
of bit positions, the Standard also describes it in purely
arithmetic form, as multiplication or division by an integer
power of two -- this latter part only makes sense when applied
to the value, not the representation, of the shifted quantity.
--