将位从一个字节移位到(数组)

时间:2011-09-29 14:52:57

标签: c bit-manipulation

我解决了这个问题,但是不知道如何以良好的方式发布它,所以我编辑这篇文章并将解决方案放在最后。


在C中需要帮助,尝试将字节位移到逆序。

我希望Step1[] = {1,0,0,1,0,0,0,0};成为{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;
    }

我知道bridcontrol是完全错误的,在这里我需要帮助! 亲切的问候


新代码:

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];
}

解决方案:

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


}

如果我想要数组中位的反转顺序,只需将tempvalue[8-i]更改为tempvalue[i-1]

7 个答案:

答案 0 :(得分:2)

您的变量名称听起来像是在尝试使用硬件。所以我想你真的想要在一个字节变量中移位而不是在一个int数组中。

此语句反转一个字节中的位:

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);

如果你真的想要反转int数组,你可以使用scottm建议的LINQs Reverse方法,但这可能不是最快的选择。

答案 1 :(得分:1)

使用Array.Reverse()轻松搞笑:

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

var reversed = Array.Reverse(step1);

如果您确实需要交换字节序,则可以查看答案here

答案 2 :(得分:0)

看起来像重复的How to reverse the order of a byte array in c#?

使用Array.Reverse

byte[] bytes = GetTheBytes();
Array.Reverse(bytes, 0, bytes.Length);

使用LINQ

byte[] bytes = GetTheBytes();
byte[] reversed = bytes.Reverse().ToArray();

答案 3 :(得分:0)

请查看Bit Twiddling Hacks

显而易见的方法,用于演示目的:

unsigned int v;     // input bits to be reversed
unsigned int r = v; // r will be reversed bits of v; first get LSB of v
int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end

for (v >>= 1; v; v >>= 1)
{   
  r <<= 1;
  r |= v & 1;
  s--;
}
r <<= s; // shift when v's highest bits are zero

答案 4 :(得分: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));
}

答案 5 :(得分: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

答案 6 :(得分: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];  
}