Re: Byte ordering and array access




"Vladimir S. Oka" <novine@xxxxxxxxxxxxxxx> wrote in message
news:dsithd$gt9$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
stathis gotsis wrote:
May i ask a question on this? Can the endian-ness of the processor
affect the "<<" shifting direction? From the replies i assume it does.
I need an example where this operator shifts to the right.

No, it does not affect shift "direction". It may help if you think of
shifts as repeated integer divisions/multiplications by 2 (that's how
Standard defines them -- they work on /values/ not representations).
Endianness only affect how values are stored in memory (their bit
representation, if you will). IOW, before performing the shift, C
program reads operand's representation, figures out the /value/,
performs shifting (i.e. division/multiplication), and if required
converts value back to representation, and stores it back.

I don't think you can construct the representation/endinanness
combination that will "reverse" shifts. Or, I'm not at my creative best
at the moment (a distinct possibility -- it's Friday evening, I should
be in a pub).

Well, yes there is a clear distinction between values and representations,
so my question was pointless anyway. But suppose we have 2-byte integers and
let int a=0xABCD. In one representation that could be: ABCD and in another:
DCBA. In terms of representations, one could say that in the first one "<<"
operator shifts left and in the second to the right. But that cannot happen
in the real world right?

Furthermore, let's take the OP. The program needs to evaluate the following
expression: ((unsigned int)byteArray[1] << 8). I assume that means that
byteArray[1] should move to the place of the next most significant byte in
the unsigned int word. So that shifting could be "left" or "right" depending
on endianness.


.



Relevant Pages

  • Re: some tricky questions
    ... known as the value representation." ... had a conforming C implementation because it was a long time ago. ... Shifts worked by doing what was needed to move the bits around in the ... I would be surprised if the wording you quote was intended to prevent ...
    (comp.lang.c)
  • Re: from comp.compression
    ... shifts as optimizations for multiplication and division by powers ... division by powers of two. ... characters and pointers in addition to numbers. ... representation of booleans. ...
    (comp.arch)
  • Re: Arithmetic shift operation in C
    ... > representation of non-negative integers is pretty well fixed, ... > However, what Mr. Kirby wrote, and what I objected to, is that shifts ... I'm not saying shifts work on objects (I don't think I did but if so that ... I'm saying that they work at a representation level. ...
    (comp.lang.c)
  • Re: Shift portability
    ... Shifts operate on the value, not representation. ... descussion of the finer points, have a look at this thread: ...
    (comp.lang.c)