Easy way to read selected bits from byte

I'm sure there is a simpler way of doing this:-

HVBattSOCAverage_A=0;
HVBattSOCAverage_A += (rxBuf0[7]);
HVBattSOCAverage_A += (bitRead(rxBuf0[6],0)(256));
HVBattSOCAverage_A += (bitRead(rxBuf0[6],1)
(512));
HVBattSOCAverage_A += (bitRead(rxBuf0[6],2)(1024));
HVBattSOCAverage_A += (bitRead(rxBuf0[6],3)
(2048));
HVBattSOCAverage_A += (bitRead(rxBuf0[6],4)(4096));
HVBattSOCAverage_A += (bitRead(rxBuf0[6],5)
(8192));

so I'm reading only 6 bits from 1 byte & all from the other to acquire a combined value.

Many thanks

Welcome to the forum

What exactly are you trying to do ?

HVBattSOCAverage_A += (bitRead(rxBuf0[6], 0)(256));

What exactly does this line, and others like it ,do ?

A little more detail, please.

  1. which bits of the second byte?
  2. are you actually just building a 16-bit int, but don't want 2 bits? Just & with 0x3F, then.

Not sure what you think the code you provided does. Can't check if it compiles, but I have my doubts.
FWIW, I'd simply merge the two bytes, then & the result with 0x3FFF, if you actually want the lower 14 bits.

Is that supposed to be a comment? If not, what do you think it should do?

Oh, wait: I'll bet you forgot to use code tags. We very strongly recommend that you do so. "</>" post editor button.

Okay looks like the multiply has vanished.

HVBattSOCAverage_A=0;
 HVBattSOCAverage_A += (rxBuf0[7]);
 HVBattSOCAverage_A += (bitRead(rxBuf0[6],0)*(256));
 HVBattSOCAverage_A += (bitRead(rxBuf0[6],1)*(512));
 HVBattSOCAverage_A += (bitRead(rxBuf0[6],2)*(1024));
 HVBattSOCAverage_A += (bitRead(rxBuf0[6],3)*(2048));
 HVBattSOCAverage_A += (bitRead(rxBuf0[6],4)*(4096));
 HVBattSOCAverage_A += (bitRead(rxBuf0[6],5)*(8192));

so im trying to get a value of the whole of buffer 7 & 6 bits of buffer 6.

so buffer 7 is straight forward.
buffer 6 bit 0 I'm multiplying by 256, bit 1 by 512, bit 2 by 1024, etc

If only you had used code tags in your original post

 HVBattSOCAverage_A = ((rxBuf0[6]&0x3F)<<8) + rxBuf0[7];

In English, mask off the bottom six bits of the high byte, multiply by 256 and add the low byte.

1 Like

Fantastic,
Thank you.
Yep will use tags in future.

So the above I now understand but how do I work this into a simpler format?
but somewhat stumped with this..

ModeExt_A=0; 
ModeExt_A+=(bitRead(rxBuf0[1],4));
ModeExt_A+=(bitRead(rxBuf0[1],5)*(2));
ModeExt_A+=(bitRead(rxBuf0[1],6)*(4));

so very much as above but only need bits 4,5,6

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Change the mask and shift appropriately.

I guess it is this:
ModeExt_A = (rxBuf0[1] >> 4) & 0b00000111 ;
or this alternative:
ModeExt_A = (rxBuf0[1] & 0b01110000) /16;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.