I would like to transfere the 6 first bits of "tempdata[6]" to an integrer and take the remaining two bits from "tempdata[6]" and put them infront of "tempdata[7]" and convert these 10 bits to integer.
I would like to transfere the 6 first bits of "tempdata[6]" to an integrer and take the remaining two bits from "tempdata[6]" and put them infront of "tempdata[7]" and convert these 10 bits to integer.
Do any one have an idea how I shall do this
1. Does the following diagram present what you want to achieve?
2. If yes, write very rudimentary codes to transform the actions of Fig-1 into Arduino Program
byte tempdata[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF1}; //test data
int x = 0;
for (int i = 0; i < 5; i++)
{
bitWrite(x, i, bitRead(tempdata[6], i)); //1101 1110 ; copying only 6 bits (bit-by-bit)
}
Serial.println();
for (int i = 15; i >= 0; i--)
{
Serial.print(bitRead(x, i)); //prints: 0000 0000 0001 1110
}
//=============================================
3. Now you follow the codes of Step-2 and try to write codes to construct int y from the data of tempdata[7] and tempdata[6][1:0]. When you have done, the result should be: 000000 0001 0010 11
It is clearly useful to have a grasp of bit manipulation concepts for intermediate level Arduino programming (direct port manipulation, reading sensors etc.)
Interestingly, the problem in the OP could be solved also by using conventional (non bit level) operators:
uint16_t d1 = tempdata[6] / 4 ; // first 6 high order bits of byte treated as an integer
uint16_t d2 = ( ( tempdata[6] % 4 ) * 256 ) + tempdata[7]; // 2 low order bits concatenated with a full byte