2011-09-29 142 views
4

我解決了這個問題,但不知道如何以良好的方式發佈它,所以我編輯這篇文章並將解決方案放在最後。將位從一個字節轉移到一個(數組)


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

+1

爲什麼人們會低調?這是一個合法的問題,OP在發佈之前已經明確地嘗試了一些東西(這是問題!)。 – jadarnel27

+1

你說的是位,但是顯示了一個整數數組的例子。我很困惑你究竟在做什麼。通過這個例子,它看起來像你試圖扭轉你的整數數組的順序。 –

+0

這是C#嗎?看起來更像是C#和C(uint8_t,char *)的混合... – Jan

回答

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

易於俗氣與Array.Reverse()

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

var reversed = Array.Reverse(step1); 

如果你確實需要交換字節序,你可以看看答案here

+1

請注意,Array.Reverse實際*改變了現有的數組*。如果你想在不改變原始數組的情況下使用與數組相反的新序列,則使用LINQ序列運算符庫中的Reverse()擴展方法。 –

+0

@Eric Lippert,很高興知道。 Reverse()擴展方法只是對數組進行反向迭代,對嗎?如果我在哪裏調用array.Reverse()。ToArray()'然後會發生變異? – scottm

+2

這不會改變原始數組。相反,它會創建一個包含反轉狀態的* second *數組。 (另外,我注意到你的代碼是錯誤的--Array.Reverse是無效的 - 返回。) –

2

你的變量名聽起來像你正在嘗試使用硬件。所以我想你真的想在一個字節變量中移位而不是在一個int數組中。

這statment逆轉字節位:

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

如果你真的想扭轉你可以使用LINQs Reverse方法,通過scottm但是那可能不是最快的選項,建議一個int數組。

+0

這樣的代碼太聰明瞭。除非性能被證明是一個問題,否則應首先使用Array.Sort。 – wllmsaccnt

+0

@wllmsaccnt:我不是在這裏談論一個數組。 – Jan

+0

沒錯,但他已經在他的例子中得到了他的數值。他最好的選擇是使用內置函數而不是位移操作。 – wllmsaccnt

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(); 
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 
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)); 
} 
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 
相關問題