Shift bits from byte to (array)

I solved the problem, but I don’t know how to publish it in a good manner, so I will edit this entry and put it at the end.


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

I want Step1[] = {1,0,0,1,0,0,0,0}; became {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 that bridcontrol is absolutely wrong, here I may need help! Yours faithfully


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

Decision:

 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 need the reverse order of the bits in the array, just change tempvalue[8-i] to tempvalue[i-1] .

+4
source share
7 answers
 //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]; } 
0
source

Your variable names sound like you're trying to work with equipment. Therefore, I think you really want to shift the bits into one byte variable, and not into an int array.

This status changes the bits in the 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 change the int array, you can use the LINQs Reverse method suggested by scottm, but this is probably not the fastest option.

+2
source

Easy-cheesy with Array.Reverse () :

 byte[] step1 = new {0,1,0,1}; var reversed = Array.Reverse(step1); 

If you really need to change endianess, you can see the answer here .

+1
source

Sounds like a duplicate. How do I reorder a byte array in C #?

Using Array.Reverse

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

Using LINQ

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

Take a look at Tweedling Hacks Bit :

An obvious way for demonstration purposes:

 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 highest bits are zero 
0
source
 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
source
 // 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 
0
source

All Articles