4

I solved the problem but dont know how to post it in a good manner, so I edit this post and put the solution in the end of it.


Need help with following in C, trying to shift a bytes bits to reverse order.

I want Step1[] = {1,0,0,1,0,0,0,0}; to become {0,0,0,0,1,0,0,1}.

void Stepper(void)
{
static uint8_t Step1[] = {1,0,0,1,0,0,0,0};
BridgeControl(Step1);
}

void BridgeControl(unsigned char *value)
{
    uint8_t tempValue;
    uint8_t bit = 8;
    uint8_t rev = 1;

    if (rev) // CW otherwise CCW
    {
        tempValue = *value;
        do{

        if(tempValue) // Right-shift one
            tempValue = 1 >> 1;
        else
            tempValue = 0 >> 1;

        }while(--bit, bit);
        *value = tempValue;
    }

I know the bridcontrol is totally wrong, here i could need help! Kind Regards


New code:

void BridgeControl(uint8_t *value)
{
    // For example, initial value could be 1001000 then I
    // would like the outcome to be 00001001

    uint8_t tempValue;

    uint8_t bit = 3;
    uint8_t rev = 1;

    if (rev) // CW otherwise CCW
    {
        tempValue = *value; //so... its 0b10010000
        do{
            tempValue >>=1; //1st this produce 01001000
            tempValue = 0 >> 1; //1st this produce 0010 0100
                                //2nd time produce 0001 0010
                                //3d time produce 0000 1001
        }while(--bit, bit);
    *value = tempValue;
    }
    M1BHI = value[7];
    M1BLI = value[6];
    M1AHI = value[5];
    M1ALI = value[4];
    M2BHI = value[3];
    M2BLI = value[2];
    M2AHI = value[1];
    M2ALI = value[0];
}

Solution:

void BridgeControl(uint8_t value)
{
    uint8_t tempvalue[8];
    uint8_t i = 8;
    uint8_t cont;
    cont = value;
    do{
        value = value >> i-1;
        value = value & 1;
        tempvalue[8-i] = value;
        value = cont;
    }while(--i,i);

    M1BHI = tempvalue[7]; 
    M1BLI = tempvalue[6]; 
    M1AHI = tempvalue[5]; 
    M1ALI = tempvalue[4]; 
    M2BHI = tempvalue[3]; 
    M2BLI = tempvalue[2]; 
    M2AHI = tempvalue[1]; 
    M2ALI = tempvalue[0]; 


}

If I want the reversed order of the bits in the array, just change tempvalue[8-i] to tempvalue[i-1].

15
  • 1
    Why are people downvoting? This is a legitimate question and the OP has obvisouly tried something before posting here (it's in the question!). Commented Sep 29, 2011 at 14:56
  • 1
    You talk about bits but show an example of an array of integers. I'm confused on exactly what you are trying to do. By the example it looks like you are trying to reverse the order of your integer array. Commented Sep 29, 2011 at 14:57
  • Is this C#? Looks more like a mix of C# and C (uint8_t, char*) ... Commented Sep 29, 2011 at 14:57
  • Is this valid c# code? it looks like c to me...maybe wrong tag? Commented Sep 29, 2011 at 14:59
  • He's using pointers in unsafe C# code. He is trying to write C/C++ code in C# <shrug>. To each their own... Commented Sep 29, 2011 at 14:59

5 Answers 5

2

Your variable names sounds like you are trying to work with hardware. So i guess you really want to shift bits in one byte variable and not in an int array.

This statment reverses the bits in a byte:

byte reversedVal = (byte) (val & 1 << 7
                          + val & 2 << 5
                          + val & 4 << 3
                          + val & 8 << 1
                          + val & 16 >> 1
                          + val & 32 >> 3
                          + val & 64 >> 5
                          + val & 128 >> 7);

If you really want to reverse a int array you can use LINQs Reverse method as suggested by scottm but thats probably not the fastest option.

Sign up to request clarification or add additional context in comments.

8 Comments

Code like this smacks of being too clever. Unless performance was proven to be an issue you should use Array.Sort first.
@wllmsaccnt: Im not talking about an array here.
Right, but he's already got his values in his example in an array. His best bet is to use a well understood built in function rather than bit shifting operations.
@wllmsaccnt: Depends. I think the byte array code was his (failed) try to get the bits in a byte inversed. But maybe he will clarify his real concern anytime :)
By the way, you are right. Your code is a just under 11 times faster than Array.Sort on my hardware.
|
1

Easy-cheesy with Array.Reverse():

byte[] step1 = new {0,1,0,1};

var reversed = Array.Reverse(step1);

If you actually need to swap endianess, you can look at the answer here.

3 Comments

Note that Array.Reverse actually mutates the existing array. If you want a new sequence that is the reverse of the array without changing the original array then use the Reverse() extension method from the LINQ sequence operator library.
@Eric Lippert, that's good to know. The Reverse() extension method just iterates the array in reverse, correct? If I where to call array.Reverse().ToArray() would that then mutate?
That would not mutate the original array. Instead it would create a second array that contained the reversed state. (Also, I note that your code is wrong -- Array.Reverse is void-returning.)
0
void BridgeControl(unsigned char values[], int size){
    unsigned char *head, *end, wk;
    for(head = values, end = values + size - 1; head < end; ++head, --end){
        wk = *head;
        *head = *end;
        *end = wk;
    }
}
void Stepper(void){
    static unsigned char Step1[] = {1,0,0,1,0,0,0,0};
    BridgeControl(Step1, sizeof(Step1));
}

Comments

0
// changed the parameter to suit the type of members of array
void BridgeControl(uint8_t *value)
{
    uint8_t tempvalue;

    // only need to loop till midway; consider the two sides as lateral images of each other from the center
    for (int i=0; i < 8/2; i++)
    {
        // preserve the current value temporarily
        tempvalue = *[value + i];
        // set the current value with the value in the mirrored location
        // (8th position is the mirror of 1st; 7th position is the mirror of 2nd and so on)
        *[value + i] = *[value + (7 - i)];
        // change the value in the mirrored location with the value stored temporarily
        *[value + (7 - i)] = tempvalue;
    }

}
// you may want to use sizeof(uint8_t) * (7 - i) instead of (7 - i) above

Comments

0
//Exempel of input going to function: int value = 0b10010000; 

void BridgeControl(uint8_t value uint8_t dir) 
{ 
    uint8_t tempvalue[8]; 
    uint8_t i = 8; 
    uint8_t cont; 
    cont = value;
 if (dir){
    do{ 
        value = value >> i-1; 
        value = value & 1; 
        tempvalue[8-i] = value; 
        value = cont; 
    }while(--i,i); 
 }
 else{
    do{ 
        value = value >> i-1; 
        value = value & 1; 
        tempvalue[i-1] = value; 
        value = cont; 
    }while(--i,i); 
 }
    M1BHI = tempvalue[7];  
    M1BLI = tempvalue[6];  
    M1AHI = tempvalue[5];  
    M1ALI = tempvalue[4];  
    M2BHI = tempvalue[3];  
    M2BLI = tempvalue[2];  
    M2AHI = tempvalue[1];  
    M2ALI = tempvalue[0];  
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.