little/big endian question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Sullivan

    little/big endian question.


    Suppose I have this code:

    unsigned short svalue;
    unsigned char hibyte, lobyte;

    svalue = 0xABCD;

    hibyte = (svalue >8) & 0xFF;
    lobyte = svalue & 0xFF;

    Will this result in the values of hibyte and lobyte being 0xAB and 0xCD
    respectively, regardless of whether the platform is little or big endian?

    Thanks for your help.

    Regards,
    Charles Sullivan
  • Willem

    #2
    Re: little/big endian question.

    Charles wrote:
    )
    ) Suppose I have this code:
    )
    ) unsigned short svalue;
    ) unsigned char hibyte, lobyte;
    )
    ) svalue = 0xABCD;
    )
    ) hibyte = (svalue >8) & 0xFF;
    ) lobyte = svalue & 0xFF;
    )
    ) Will this result in the values of hibyte and lobyte being 0xAB and 0xCD
    ) respectively, regardless of whether the platform is little or big endian?

    Of course it will. The shift operator doesn't care about endianess.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT

    Comment

    • Charles Sullivan

      #3
      Re: little/big endian question.

      On Sat, 03 May 2008 14:34:25 +0000, Willem wrote:
      Charles wrote:
      )
      ) Suppose I have this code:
      )
      ) unsigned short svalue;
      ) unsigned char hibyte, lobyte;
      )
      ) svalue = 0xABCD;
      )
      ) hibyte = (svalue >8) & 0xFF;
      ) lobyte = svalue & 0xFF;
      )
      ) Will this result in the values of hibyte and lobyte being 0xAB and
      0xCD ) respectively, regardless of whether the platform is little or big
      endian?
      >
      Of course it will. The shift operator doesn't care about endianess.
      >
      >
      SaSW, Willem
      Thanks Willem. That had been my understanding but some strange bug
      reports from users got me wondering whether I was mistaken.

      Regards,
      Charles Sullivan

      Comment

      • vippstar@gmail.com

        #4
        Re: little/big endian question.

        On May 3, 5:45 pm, Charles Sullivan <cwsul...@triad .rr.comwrote:
        On Sat, 03 May 2008 14:34:25 +0000, Willem wrote:
        Charles wrote:
        )
        ) Suppose I have this code:
        )
        ) unsigned short svalue;
        ) unsigned char hibyte, lobyte;
        )
        ) svalue = 0xABCD;
        )
        ) hibyte = (svalue >8) & 0xFF;
        ) lobyte = svalue & 0xFF;
        )
        ) Will this result in the values of hibyte and lobyte being 0xAB and
        0xCD ) respectively, regardless of whether the platform is little or big
        endian?
        >
        Of course it will. The shift operator doesn't care about endianess.
        >
        SaSW, Willem
        >
        Thanks Willem. That had been my understanding but some strange bug
        reports from users got me wondering whether I was mistaken.
        Then perhaps the bug lies somewhere else?
        From ISO 9899:1999, 6.5.7:
        The result of E1 >E2 is E1 right-shifted E2 bit positions.
        If E1 has an unsigned type or if E1 has a signed type and a
        nonnegative value, the value of the result is the integral part
        of the quotient of E1 / 2^(E2) . If E1 has a signed type and a
        negative value, the resulting value is implementation-defined.
        (int)(0xABCD / pow(2, 8)) == 0xAB (== 171)

        Comment

        • Martin

          #5
          Re: little/big endian question.

          On Sat, 03 May 2008 15:29:24 +0100, Charles Sullivan
          <cwsulliv@triad .rr.comwrote:
          hibyte = (svalue >8) & 0xFF;
          What's the point of the '& 0xFF'?

          --
          Martin

          Comment

          • Ben Bacarisse

            #6
            Re: little/big endian question.

            Martin <martindotounde rscorebrien@whi ch.netwrites:
            On Sat, 03 May 2008 15:29:24 +0100, Charles Sullivan
            <cwsulliv@triad .rr.comwrote:
            > hibyte = (svalue >8) & 0xFF;
            >
            What's the point of the '& 0xFF'?
            Well, it does make a difference if unsigned char has more than 8 bits
            and unsigned short has more than 16. If you've used a machine with
            36, 18 and 9 bit types to implement octet-based processing, you do
            this automatically.

            --
            Ben.

            Comment

            Working...