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

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Christian
  • 1,548
  • 2
  • 15
  • 26
  • 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!). – Josh Darnell Sep 29 '11 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. – Bobby Cannon Sep 29 '11 at 14:57
  • Is this C#? Looks more like a mix of C# and C (uint8_t, char*) ... – Jan Sep 29 '11 at 14:57
  • Is this valid c# code? it looks like c to me...maybe wrong tag? – Luis Sep 29 '11 at 14:59
  • He's using pointers in unsafe C# code. He is trying to write C/C++ code in C# . To each their own... – keithwill Sep 29 '11 at 14:59
  • Looks like several duplicates, unless I'm misunderstanding http://stackoverflow.com/questions/5784365/how-to-reverse-the-order-of-a-byte-array-in-c – Greg B Sep 29 '11 at 15:05
  • Looks like a duplicate http://stackoverflow.com/questions/5784365/how-to-reverse-the-order-of-a-byte-array-in-c – Greg B Sep 29 '11 at 15:07
  • I think he really want to inverse bits in a byte, The code posted seems to be misleading. – Jan Sep 29 '11 at 15:09
  • Ironic how many 'righteous' commenters get it wrong. This is C, and uint8_t is in [stdint.h](http://linux.die.net/man/3/uint8_t). Nobody is trying to do unnaturals acts against C#. Just get used to people programming in other languages than yourself :) (_I DID google for the meaning of **bridcontrol** for a minute..._) – sehe Sep 29 '11 at 22:30
  • Thanks, acctually I made my own function for uint8_t, doh Im programming in C18. Include "inttypes.h" #ifdef __18CXX typedef unsigned char uint8_t; // 0 -> 255 // Unsigned Char #else # include #endif – Christian Sep 30 '11 at 06:49
  • Oh... and by doing the above, I have no probling sorting it to oposite – Christian Sep 30 '11 at 12:17
  • @sehe: it used to be tagged [tag:c#] and has "C#" in the title. – Joachim Sauer Sep 30 '11 at 12:18
  • @JoachimSauer: aha. Objection withdrawn :) `@`Christian: [C18](http://www.google.nl/url?sa=t&source=web&cd=2&ved=0CCQQFjAB&url=https%3A%2F%2Fwww.nema.org%2Fstds%2Fcomplimentary-docs%2Fupload%2FANSI%2520C18.1M%2520Part%25202.pdf&ei=Mr-FToz4FZDG-Qbo8swg&usg=AFQjCNE00Iz8aVGZ4AAswunJBioeGG8mQw&sig2=mCjf3F2WR22RyRjjn1XHgg) ? AFAICT that has to do with batteries... (see http://en.wikipedia.org/wiki/ANSI_C) – sehe Sep 30 '11 at 13:07
  • @Christian, you should post your answer as an answer, not edit the question. You can then accept that (instead of adding “solved” to the title). – svick Sep 30 '11 at 13:57
  • haha... did'nt see that the button changed to Answer question :P – Christian Sep 30 '11 at 16:25

5 Answers5

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.

Jan
  • 15,802
  • 5
  • 35
  • 59
  • Code like this smacks of being too clever. Unless performance was proven to be an issue you should use Array.Sort first. – keithwill Sep 29 '11 at 15:10
  • @wllmsaccnt: Im not talking about an array here. – Jan Sep 29 '11 at 15:12
  • 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. – keithwill Sep 29 '11 at 15:16
  • @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 :) – Jan Sep 29 '11 at 15:18
  • By the way, you are right. Your code is a just under 11 times faster than Array.Sort on my hardware. – keithwill Sep 29 '11 at 15:39
  • I'm sorry, it's C for microprocessor! And what I try to accomplice is stated in the begining of my post I try to reverse order of bits in my array. Sorry if I was unclear. A function is easier to understand for a noob as me, but an example of some smart bitwise operation would be nice! – Christian Sep 29 '11 at 17:16
  • So you have an array of byte and each byte represents a bit and you want to inverse the bytes in the array? Is that right? Why aren't you using one byte with 8 bits? Especially in microcontrollers every bit you can save counts! – Jan Sep 29 '11 at 18:37
  • Mabe cause I dont know better? Small exemple that could work that I could need som better suggestions on will be given in my first post – Christian Sep 29 '11 at 20:03
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.

Community
  • 1
  • 1
scottm
  • 27,829
  • 22
  • 107
  • 159
  • 1
    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 Sep 29 '11 at 15:04
  • @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? – scottm Sep 29 '11 at 15:06
  • 2
    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.) – Eric Lippert Sep 29 '11 at 15:09
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));
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
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
Arun
  • 2,493
  • 15
  • 12
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];  
} 
Christian
  • 1,548
  • 2
  • 15
  • 26