PDA

View Full Version : Remove bits from a byte



ituarte
07-22-2015, 07:59 AM
Hello,

I have a byte giving from a Temp. Sensor in Binary.

To change from binary to dec. first I should remove the first 3 bits of the byte and the 2 last bits of the LSB byte.

I mean,

If I have on the MSB 11000001 I need remove the first 3 bits and keep the value 000001
And if I have on the LSB 10101110 I need remove the last 2 bits and keep the value 101011



Someone can help me to do it??

Thanks in advance

p45cal
07-22-2015, 08:18 AM
How do you know whether a byte is MSB or LSB?

Perhaps a file with 10 or so examples (that's one more than nine, not 10 in binary!)

Paul_Hossler
07-22-2015, 08:28 AM
Try this to see if it does what you want

This is masking out the bits. Doing left and right shifts is a little trickier

As aside, you said you "have a byte giving...". Since it looks like there is a Most and a Least, are you sure your not getting a 16 bit word? Don't know if it matters






Option Explicit
'I have a byte giving from a Temp. Sensor in Binary.
'To change from binary to dec. first I should remove the first 3 bits of the byte and the 2 last bits of the LSB byte.
'If I have on the MSB 11000001 I need remove the first 3 bits and keep the value 000001
'And if I have on the LSB 10101110 I need remove the last 2 bits and keep the value 101011
Sub test()
Dim MSB As Byte, LSB As Byte, MaskOutFirst3 As Byte, MaskOutLast2 As Byte

MSB = 193 ' 11000001
LSB = 174 ' 10101110

MaskOutFirst3 = 1 + 2 + 4 + 8 + 16
MaskOutLast2 = 128 + 64 + 32 + 16 + 8 + 4

MSB = MSB And MaskOutFirst3
LSB = LSB And MaskOutLast2


MsgBox MSB
MsgBox LSB

End Sub

ituarte
07-23-2015, 01:13 AM
Thanks, that is what I was looking for!!!