Bit Shifting problem with endieness - Unix

This is a discussion on Bit Shifting problem with endieness - Unix ; Hi, I am having the following problem of bit shifting. My program runs on a little endian machine. Consider that if I have the following data represented in big endian... 0x12345678 the little endian representation would be 0x78563412. Now if ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Bit Shifting problem with endieness

  1. Bit Shifting problem with endieness

    Hi,

    I am having the following problem of bit shifting.

    My program runs on a little endian machine. Consider that if I have
    the following data represented in big endian...

    0x12345678

    the little endian representation would be

    0x78563412.

    Now if I wan to shift my original data by 4 bits to 0x01234567, then
    my approach would be

    val = 0x12345678
    htonl(htonl(val)>>4)

    and result represented in my machine is

    0x67452301.

    This looks quite redundant to convert little endian to Bid Endian
    shift it and then convert back. Basically I thinking of how to get rid
    of those two htonl system calls.

    I would like to know is there any better way to do this. A best
    possibility to shift in little endian mode??



  2. Re: Bit Shifting problem with endieness

    Madhur wrote in
    news:50630252-c261-492a-8f6d-9a727a0afe4b@s19g2000prg.googlegrou
    ps.com:

    > Hi,
    >
    > I am having the following problem of bit shifting.
    >
    > My program runs on a little endian machine. Consider that if
    > I have the following data represented in big endian...
    >
    > 0x12345678
    >
    > the little endian representation would be
    >
    > 0x78563412.
    >
    > Now if I wan to shift my original data by 4 bits to
    > 0x01234567, then my approach would be
    >
    > val = 0x12345678
    > htonl(htonl(val)>>4)
    >
    > and result represented in my machine is
    >
    > 0x67452301.
    >
    > This looks quite redundant to convert little endian to Bid
    > Endian shift it and then convert back. Basically I thinking
    > of how to get rid of those two htonl system calls.
    >
    > I would like to know is there any better way to do this. A
    > best possibility to shift in little endian mode??


    Not sure what you mean here, and perhaps there is some confusion.

    Regardless of the endianness of the processor, the 32-bit value
    0x12345678 will be represented as 0x12345678. Thus, if you want
    to shift the value right by four bits, and assuming that int is
    a 32-bit value:

    int val = 0x12345678;
    int shifted = val >> 4;

    will do the trick irrespective of the endianness of the processor.

    Endianness only comes into the picture if you need to map a multi-
    byte quantity onto individual bytes. On a big-endian processor,
    0x12345678 is stored in consecutive bytes as 0x12 0x34 0x56 0x78,
    while on a little-endian processor it is stored as 0x78 0x56 0x34
    0x12.

    This is generally only relevant when using a buffer to transfer
    multi-byte data between applications running on processors with
    different endianness. You don't need to worry about it when
    manipulating ints, longs, etc. in your program.

    MV

    --
    I do not want replies; please follow-up to the group.

  3. Re: Bit Shifting problem with endieness

    On Mar 10, 5:34 pm, Martin Vuille wrote:
    > Madhur wrote innews:50630252-c261-492a-8f6d-9a727a0afe4b@s19g2000prg.googlegrou
    > ps.com:
    >
    >
    >
    > > Hi,

    >
    > > I am having the following problem of bit shifting.

    >
    > > My program runs on a little endian machine. Consider that if
    > > I have the following data represented in big endian...

    >
    > > 0x12345678

    >
    > > the little endian representation would be

    >
    > > 0x78563412.

    >
    > > Now if I wan to shift my original data by 4 bits to
    > > 0x01234567, then my approach would be

    >
    > > val = 0x12345678
    > > htonl(htonl(val)>>4)

    >
    > > and result represented in my machine is

    >
    > > 0x67452301.

    >
    > > This looks quite redundant to convert little endian to Bid
    > > Endian shift it and then convert back. Basically I thinking
    > > of how to get rid of those two htonl system calls.

    >
    > > I would like to know is there any better way to do this. A
    > > best possibility to shift in little endian mode??

    >
    > Not sure what you mean here, and perhaps there is some confusion.
    >
    > Regardless of the endianness of the processor, the 32-bit value
    > 0x12345678 will be represented as 0x12345678. Thus, if you want
    > to shift the value right by four bits, and assuming that int is
    > a 32-bit value:
    >
    > int val = 0x12345678;
    > int shifted = val >> 4;
    >
    > will do the trick irrespective of the endianness of the processor.
    >
    > Endianness only comes into the picture if you need to map a multi-
    > byte quantity onto individual bytes. On a big-endian processor,
    > 0x12345678 is stored in consecutive bytes as 0x12 0x34 0x56 0x78,
    > while on a little-endian processor it is stored as 0x78 0x56 0x34
    > 0x12.
    >
    > This is generally only relevant when using a buffer to transfer
    > multi-byte data between applications running on processors with
    > different endianness. You don't need to worry about it when
    > manipulating ints, longs, etc. in your program.
    >
    > MV
    >
    > --
    > I do not want replies; please follow-up to the group.


    I am sorry for insufficient data. I am trying to manupulate a buffer
    neither of ints and longs. As mentioned if I have a large buffer say
    1K bytes to shift by 4 bits, the problem definitely persists.

  4. Re: Bit Shifting problem with endieness

    On Mon, 10 Mar 2008 05:45:05 -0700 (PDT) Madhur wrote:
    > I am sorry for insufficient data. I am trying to manupulate a buffer
    > neither of ints and longs. As mentioned if I have a large buffer say
    > 1K bytes to shift by 4 bits, the problem definitely persists.


    then you are stuck.

  5. Re: Bit Shifting problem with endieness

    Madhur wrote:
    > Hi,
    >
    > I am having the following problem of bit shifting.
    >

    Please don't multi-post on Usenet, your question has been answered on c.l.c.

    --
    Ian Collins.

  6. Re: Bit Shifting problem with endieness

    On Mar 10, 5:20 am, Madhur wrote:

    > This looks quite redundant to convert little endian to Bid Endian
    > shift it and then convert back. Basically I thinking of how to get rid
    > of those two htonl system calls.
    >
    > I would like to know is there any better way to do this. A best
    > possibility to shift in little endian mode??


    It sounds like you really need to do three different things:

    1) You need to convert the input data from its native format into a
    form in which you can manipulate it.

    2) You need to manipulate the data.

    3) You need to convert the processed data from the form in which you
    can manipulate it into the desired output format.

    So it sounds like you really do need all three steps, but that you are
    perhaps visualizing the problem badly.

    DS

+ Reply to Thread