1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Rotate 8 bits for 3 to right in p. language C

Discussion in '8051/8951' started by 8051help, Feb 9, 2016.

  1. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0
    Hi.

    I need to rotate for an example x=0XFF for 3 to right.

    I cant do that only with >> because that is shifting.
    When you want rotate you need to "memory" all bit not to lose such as in case for shifting (i think).
    So that is not the same.

    Can I use this (x & 0xFF)>>3 ; for rotate ??
     
  2. Wade_Hassler

    Wade_Hassler New Member

    Joined:
    Nov 19, 2013
    Messages:
    24
    Likes:
    2
    Your compiler might do what you want if the number is declared as signed
    Or if it is unsigned....
    Code (text):

    if( x & 128)  x = ( x>>3 )| 128;
    else          x = ( x>>3);
     
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    If you are rotating you need to know the condition of bit 0
    Code (c):

    shiftbits = 3;
    while(shiftbits--)
       {
       if (rotatebyte && ox1)
          {
          rotatebyte>>= 1;
          rotatebyte+=128;
          }
       else
           rotatebyte>>=1;
       }
     
     
    • Agree Agree x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0

    when i done like

    Code (text):

    if( x & 128){  x = ( x>>3 )| 128;
              display=x;   }
    else  x = ( x>>3);
                     display=x; // display is memory mapped at adress 0x8000
     

    the result is not good..


    ANOTHER post with rotatebyte doesnt work

    Thank you guys but doesnt work

    i working in micro vision 3 atmel 8051
     
  6. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0
    May I use this
    Code (text):

    (x>>3) | (x<<( (-3) & 0xFF ));

     
     
  7. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0
    When I use (x>>3) | (x<<( (-3) & 0xFF )); and classic shifting I got the same result . Maybe because all diodes are 1111111???
     
  8. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0
    I have result

    x=0xFF;
    (((x>>3)&0x1f)|(x<<5));
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    That is one way, but its not portable..... If you want to rotate 4 times you'll need more code

    I had a typo in my code sorry
    Code (c):

    shiftbits = 3;
    while(shiftbits--)
       {
       if (rotatebyte & 0x1) // Should have been bit wise!!!
          {
          rotatebyte>>= 1;
          rotatebyte+=128;
          }
       else
           rotatebyte>>=1;
       }
     
     
    • Agree Agree x 1
  10. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0
    when I write that code in interrupt rutine i have problem with execution...

    Code (text):

    if (++counter=20) {  //i have error c213 : left side of asn-op not an lvalue THAT ERROR SHOWS  THIS LINE OF CODE
    shiftbits = 3;
    while(shiftbits--)
      {
      if (x & 0x1) // Should have been bit wise!!!
      {
      x>>= 1;
      x+=128;
        display=x;
      }
      else
      x>>=1;
      }
       counter=0;        }
       
    }
     
    i have error c213 : left side of asn-op not an lvalue
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Have you declared "shiftbits" and "x" ???

    I write two functions... ROTR and ROTL both can take byte and word arguments!!
     
  12. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0

    Yes, I did.

    shiftbits is byte and x is byte
     
  13. 8051help

    8051help New Member

    Joined:
    Nov 27, 2015
    Messages:
    27
    Likes:
    0
    I have one more question .

    if (!((--counter )+=2)) { a=1}

    in that case , what would be value of counter for a=1???
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Ah!! Remember that 8051 has limited "bit access" memory!!

    if NOT ( pre increment counter) +2 .... Will never be true unless it started out negative..

    Assume counter = 1.....
    pre increment counter (before evaluation ) counter = 2
    counter += 2 counter now contains 4 ....
    if NOT.... well!! the only time this will be true is if the count was -3 to start with

    The only time a = 1 is when counter = -3...
     
    • Agree Agree x 1
  15. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    Code (c):
    int8_t rotate_byte_right(int8_t x, int8_t pos) {
      return (x >> pos) | (x << (8 - pos));
    }

    int16_t rotate_word_right(int16_t x, int8_t pos) {
      return (x >> pos) | (x << (16 - pos));
    }

    any_int_type rotate_anything_right(any_int_type x, int8_t pos) {
      return (x >> pos) | (x << (sizeof(x)*8 - pos));
    }
     
    It'll be totally different if you do it in assembler.
     
    • Agree Agree x 1
  16. Mikebits

    Mikebits Well-Known Member

    Joined:
    May 24, 2008
    Messages:
    6,217
    Likes:
    175
    Location:
    San Diego, Ca
    I feel silly for asking, but I don't understand what you guys mean by rotating a byte, could someone explain what you mean by rotate a byte?

    Thanks
     
  17. Les Jones

    Les Jones Well-Known Member

    Joined:
    May 15, 2015
    Messages:
    1,474
    Likes:
    191
    Location:
    Lancashire UK
    Hi Mike,
    To complicate things further there is rotate and rotate through carry. This is my undersanding of "rotate". (Right) Bit 7 to bit 6 and so on. Bit 0 to bit 7. A rotate through carry is carry bit to bit 7, bit 7 to bit 6 and so on bit 0 to carry bit.

    Les.
     
  18. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Rotation is same as shifting, except that the bits that drop out at one end appears at the other end.

    You have to be careful when shifting signed integers. Arithmetic shift to the right add 1's instead of zeros to the left. I edited the code to cast right shift to unsigned.
     
    Last edited: Mar 4, 2016
  19. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    That's a good point. Many compilers do this because the geniuses who wrote the standard didn't define the behaviour.

    An easy work around is to re-define the arguments as unsigned. The "pos" argument should also be unsigned:

    Code (c):
    uint8_t rotate_byte_right(uint8_t x, uint8_t pos) {
      return (x >> pos) | (x << (8 - pos));
    }

    uint16_t rotate_word_right(uint16_t x, uint8_t pos) {
      return (x >> pos) | (x << (16 - pos));
    }

    any_unsigned_int_type rotate_anything_right(any_unsigned_int_type x, uint8_t pos) {
      return (x >> pos) | (x << (sizeof(x)*8 - pos));
    }
     

Share This Page