Continue to Site

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.

  • 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.

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

Status
Not open for further replies.

8051help

New Member
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 ??
 
Your compiler might do what you want if the number is declared as signed
Or if it is unsigned....
Code:
if( x & 128)  x = ( x>>3 )| 128;
else          x = ( x>>3);
 
when i done like

Code:
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
 
When I use (x>>3) | (x<<( (-3) & 0xFF )); and classic shifting I got the same result . Maybe because all diodes are 1111111???
 
I have result

x=0xFF;
(((x>>3)&0x1f)|(x<<5));
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
C:
shiftbits = 3;
while(shiftbits--)
   {
   if (rotatebyte & 0x1) // Should have been bit wise!!!
      {
      rotatebyte>>= 1;
      rotatebyte+=128;
      }
   else
       rotatebyte>>=1;
   }
 
when I write that code in interrupt rutine i have problem with execution...

Code:
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
 
I have one more question .

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

in that case , what would be value of counter for a=1???
 
Yes, I did.

shiftbits is byte and x is byte
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...
 
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.
 
If you are rotating you need to know the condition of bit 0
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
 
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.
 
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
Rotation is same as shifting, except that the bits that drop out at one end appears at the other end.

C:
int8_t rotate_byte_right(int8_t x, int8_t pos) {
  return (((uint8_t)x) >> pos) | (x << (8 - pos));
}
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:
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.

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:

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));
}
 
Status
Not open for further replies.

Latest threads

Back
Top