I write method to bit shift by one in array and save to second array. Let me explain on below example:
unsigned int firstNumber[2] = { 0x00000001, 0x8FFFFFFF };
unsigned int resultNumber[2];
And result should be:
unsigned int resultNumber[2] = { 0x00000000, 0xC7FFFFFF };
Below this is my function:
void bitShiftByOneInRight_1(unsigned int firstNumber[2], unsigned int resultNumber[2])
{
unsigned int lastBitEnabled = 0;
unsigned int mask = 1;
for (unsigned int i = 0; i < 2; i++) {
resultNumber[i] = 0;
// example 11001101 & 00000001 for uint8_t
unsigned int lastBit = (firstNumber[i] & mask) << (sizeof(unsigned int)*8 - 1);
resultNumber[i] = firstNumber[i] >> 1;
resultNumber[i] = firstNumber[i] | lastBitEnabled;
lastBitEnabled = lastBit;
}
}
I get answer as the same array firstNumber[2]:
unsigned int resultNumber[2] = { 0x00000001, 0x8FFFFFFF };
Simple example, we have array:
[0b00000001, 0b01111111]
then answer should be
[0b00000000, 0b10111111]
Furthermore when function is this form, which mean use one argument as input and output method, work correctly:
void bitShiftByOneInRight(unsigned int firstNumber[2]) {
unsigned int lastBitEnabled = 0;
unsigned int mask = 1;
for (unsigned int i = 0; i < 2; i++) {
// example 11001101 & 00000001 for uint8_t
unsigned int lastBit = (firstNumber[i] & mask) << (sizeof(unsigned int)*8 - 1);
firstNumber[i] = firstNumber[i] >> 1;
firstNumber[i] = firstNumber[i] | lastBitEnabled;
lastBitEnabled = lastBit;
}
}
Anyone know what I make wrong? Each answer is welcome for me! Thanks