Efficient shifting of a flat buffer

Discussion in 'C Programming' started by Madhur, Mar 3, 2008.

  1. Madhur

    Madhur Guest

    I would like to know the best efficient way to shift a flat buffer by
    say 4bits.

    for exaple if the flat buffer is

    0x62 0x48 0x23 ....

    then the result should be

    0x06 0x24 0x82 0x3.....

    byte by byte shifting somehow looked very inefficient.


    Something like
    int main()
    {
    unsigned char sample1[50];
    unsigned char sample2[50];
    int i;

    for(i=0; i < 50; i++)
    {
    sample1 = rand(250);
    }

    for(i=0 ; i < 50; i++)
    {
    printf("%02x \n",sample1);
    }

    printf(" output after shifting\n");
    sample2[0] = sample1[0]>>4;
    for(i=1; i <= 50; i++)
    {
    sample2 = (sample1[i-1] << 4) | (sample1>>4);
    printf("%02x \n",sample2);
    }

    return 0;
    }

    Is there any other best way to do this.
     
    Madhur, Mar 3, 2008
    #1
    1. Advertising

  2. Madhur

    Zara Guest

    On Mon, 3 Mar 2008 01:16:31 -0800 (PST), Madhur <>
    wrote:

    >I would like to know the best efficient way to shift a flat buffer by
    >say 4bits.
    >
    >for exaple if the flat buffer is
    >
    >0x62 0x48 0x23 ....
    >
    >then the result should be
    >
    >0x06 0x24 0x82 0x3.....
    >
    >byte by byte shifting somehow looked very inefficient.
    >
    >
    >Something like
    >int main()
    >{
    > unsigned char sample1[50];
    > unsigned char sample2[50];
    > int i;
    >
    > for(i=0; i < 50; i++)
    > {
    > sample1 = rand(250);
    > }
    >
    > for(i=0 ; i < 50; i++)
    > {
    > printf("%02x \n",sample1);
    > }
    >
    > printf(" output after shifting\n");
    > sample2[0] = sample1[0]>>4;
    > for(i=1; i <= 50; i++)
    > {
    > sample2 = (sample1[i-1] << 4) | (sample1>>4);
    > printf("%02x \n",sample2);
    > }
    >
    > return 0;
    >}
    >
    >Is there any other best way to do this.


    reducing the number or reads:

    unsigned char last=0;
    for (int i=0,i<sizeof(sample);++i) {
    unsigned char next=sample1;
    sample2= (last<<4)|(next>>4);
    last=next;
    }

    But you should profile to be sure it is better

    Zara
     
    Zara, Mar 3, 2008
    #2
    1. Advertising

  3. Madhur

    Bartc Guest

    "Madhur" <> wrote in message
    news:...
    >I would like to know the best efficient way to shift a flat buffer by
    > say 4bits.
    >
    > for exaple if the flat buffer is
    >
    > 0x62 0x48 0x23 ....
    >
    > then the result should be
    >
    > 0x06 0x24 0x82 0x3.....
    >
    > byte by byte shifting somehow looked very inefficient.
    >....


    If the hex digits were strung all together, then the above does a right
    shift. Your code however seems to do a left shift.

    One solution would be to shift 32 or 64 bits at a time (depends on your
    machine), but the way you've defined your shift means this results in very
    peculiar shift pattern.

    If you explained the reason for the shift further then perhaps we could help
    better.

    --
    Bart
     
    Bartc, Mar 3, 2008
    #3
  4. Madhur

    SM Ryan Guest

    Madhur <> wrote:
    # I would like to know the best efficient way to shift a flat buffer by
    # say 4bits.
    #
    # for exaple if the flat buffer is
    #
    # 0x62 0x48 0x23 ....
    #
    # then the result should be
    #
    # 0x06 0x24 0x82 0x3.....
    #
    # byte by byte shifting somehow looked very inefficient.

    Unless the hardware provides bit vectors, you're going to be
    doing aload/shift/mask/or/store loop. A byte at a time is least
    likely efficient, but most portable. Depending on alignment and
    endianness you can sometimes do it a word at a time, but that
    will only work on specific kinds of hardware.

    Also some hardware provides a rotate instruction implemented
    with a <<< or >>> operator. The two shifts can be replaced
    with one rotate if available.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I hope it feels so good to be right. There's nothing more
    exhilarating pointing out the shortcomings of others, is there?
     
    SM Ryan, Mar 4, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,435
    John Harrison
    Jun 21, 2004
  2. Alfonso Morra
    Replies:
    5
    Views:
    400
    Michael Wojcik
    Oct 4, 2005
  3. Alfonso Morra
    Replies:
    0
    Views:
    387
    Alfonso Morra
    Oct 3, 2005
  4. Replies:
    2
    Views:
    607
    sergejusz
    Mar 26, 2007
  5. Douglas Alan
    Replies:
    12
    Views:
    2,431
    Piet van Oostrum
    Jul 15, 2009
Loading...

Share This Page