Moving Bits in Array of Bytes

Ray Hall

New member
Joined
Sep 9, 2018
Messages
2
Programming Experience
10+
Hello,

I am trying to move the bits from one byte to another forward or backwards.

Example... I want to move Bit 1 on the sig[] array forward three places. I call
the function "setCamPostion" like this setCamPostion(3.1);

This moves the bit 1 values (0 or 2) correctly,

When I try to move the Bit 2 like this setCamPostion(3.2); it does not move them.

Below is my code.

Thank you

Ray.


C#:
#define CYCLE 20
uint8_t camsLoaded = false;
uint8_t sig[CYCLE] = {0,2,6,5,1,0,2,6,5,1,0,2,6,5,1,0,2,6,5,1};

/***********************  sig array makeup *****************************

   Bit 0 State 0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1
   Bit 1 State 0,2,2,0,0,0,2,2,0,0,0,2,2,0,0,0,2,2,0,0
   Bit 2 State 0,0,4,4,0,0,0,4,4,0,0,0,4,4,0,0,0,4,4,0

Total for sig  0,2,6,5,1,0,2,6,5,1,0,2,6,5,1,0,2,6,5,1

************************************************************************/

                  

//---------- Set Cam Position ------------------------

void setCamPostion(int pos, uint8_t cam)
{
   uint16_t x, y;
   static int cam1LastPos = 80;
   static int cam2LastPos = 80;
   static int cam3LastPos = 80;  
   
   static uint8_t camX[CYCLE];
   uint8_t mask;

   if (camsLoaded == false) { // if cam patterns not loaded into static array
     for (x = 0; x < CYCLE; x++) {
        camX[x] = sig[x];
     }   
     camsLoaded = true;     
   }
   
   
   mask = (1<<cam);
       
   if (pos >= 0) 
      y = pos;   
   else 
      y = CYCLE + pos;

   for (x = 0; x < CYCLE; x++) {
     if (camX[y] & mask) 
        sig[x] |= mask;
     else 
        sig[x] &= ~mask;


     if (++y >= CYCLE)
        y = 0;
   }
      
   switch (cam) {
      case 1:  cam1LastPos = pos;
           break;
      case 2:  cam2LastPos = pos;
           break;
      case 3:  cam3LastPos = pos;
           break;
      case 4:  cam4LastPos = pos;
           break;           
   }                                                            
}
 
There is no problem with the code. It works perfectly. The problem was elsewhere in the code. I had two things calling this function. One I was not aware of and it was causing me the confusion.

I always find that when I am stuck with a bug I am looking in the wrong area for the answer. I get tunnel vision...

Thanks anyway.

Ray.
 
Back
Top Bottom