Reading value of a bit?

G

gene kelley

I have an application where I need to read some header information found in 3
different types of file types. Two of the file types were fairly straight forward as
the items to read in the header are at least 8 bits (one byte), so, I'm able to step
through a file stream with a binary reader and retrive the data. The last file type,
however, has me stumped at the moment. The header spec specifies the item lengths in
bits. Most of the item lengths are 8 bit multiples which is not a problem. There
are three items, however, that are listed with less than 8 bit lengths - one as 1
bit, one as 7 bit, and another at 3 bits.

In VB, how do you read bit values?

Gene
 
C

Cor Ligthert [MVP]

Gene,

And that on a Microsoft OS System. In my idea is the smallest item you can
read a byte.

Cor
 
B

Branco Medeiros

gene said:
I have an application where I need to read some header information found in 3
different types of file types. Two of the file types were fairly straight forward as
the items to read in the header are at least 8 bits (one byte), so, I'm able to step
through a file stream with a binary reader and retrive the data. The last file type,
however, has me stumped at the moment. The header spec specifies the item lengths in
bits. Most of the item lengths are 8 bit multiples which is not a problem. There
are three items, however, that are listed with less than 8 bit lengths - one as 1
bit, one as 7 bit, and another at 3 bits.

In VB, how do you read bit values?

Usually it consists on using the And operator and the Shift operators
(<< and >>). But, in the end, it all will depend on how the sequence of
elements is layed out in the byte stream.

Just to give you a starting point, consider that to read an arbitrary
amount of bits from a byte you need first to isolate the relevant bits
and align them to the lower boundary of the byte.

Suppose for instance you have a 3-bit value. The easier situation is
when the bits are already "right-aligned" in the byte:

aaaaabbb

(where 'a' represents "don't care" bits, and 'b' represents the bits
you want).

In this case you need only to mask the relevant bits with an And-mask,
that is, a sequence of n 1 bits, where n is the size of your data -- 3,
in this case:

aaaaabbb And 00000111 = 00000bbb

How to find the mask, you ask? Easy:

Dim Mask As Byte = (1 << Size) - 1

The first problem begins when the bits are not 'right-aligned' in the
byte:

aaabbbaa

To resolve this you must first put the bits in the 'right' position.
You do this by shifting the bits to right n positions, where n is the
leftmost position of the bit pattern plus one, minus the size of the
pattern:

Dim Shift As Byte = LeftmostBit + 1 - Size
Value = Value >> Shift

Notice, however, that the bit positions are numbered like this:

76543210

Therefore, in the previous example, the leftmost bit would be 4.

So, to put it all together so far you'd have something like:

<aircode>
Function GetBits(Value as Byte, _
LeftmostBit As Byte, _
Size as Byte) As Byte
Dim Shift As Byte = LeftmostBit + 1 - Size
Dim Mask As Byte = (1 << Size) - 1
Return (Value >> Shift) And Mask
End Function
</aircode>

Probably, depending on your settings, VB will give you all types of
warnings, because in the above expressions the value "1" is computed as
an integer, and the entire expression is promoted to integer. Getting
rid of the warnings remains as an exercise =))

Finally, things become *really* complicated when you have a stream of
values whose bits span two bytes:

aaaaaabb baaaaaaaa

To resolve this, you must treat the two bytes as a 16 bit value (a
Short, in VB). When you do this, everything else Just Works (TM), as
long as you treat the leftmost bit position as a number from 0 to 15
(and not 0 to 7, as in the previous example). If we use hexadecimal to
represent the bit positions, we'd have:

FEDCBA98 76543210

Therefore, in the above example, our leftmost bit would be 9 !

A function that would extract a given bit pattern from a sequence of
two bytes could be like this:

<aircode>
Function GetBits(FirstByte As Byte, _
ScndByte As Byte, _
LeftmostBit As Byte, _
Size As Byte) As Byte
Dim Value As Integer = (CInt(FirstByte) << 8) Or ScndByte
Dim Shift As Integer = LeftmostBit + 1 - Size
Dim Mask As Integer = (1 << Size) - 1
Dim Result As Integer = (Value >> Shift) And Mask
Return CByte(Result And 255)
End Function
</aircode>

HTH.

Regards,

Branco.
 
G

gene kelley

Usually it consists on using the And operator and the Shift operators
(<< and >>). But, in the end, it all will depend on how the sequence of
elements is layed out in the byte stream.

Just to give you a starting point, consider that to read an arbitrary
amount of bits from a byte you need first to isolate the relevant bits
and align them to the lower boundary of the byte.

Suppose for instance you have a 3-bit value. The easier situation is
when the bits are already "right-aligned" in the byte:

aaaaabbb

(where 'a' represents "don't care" bits, and 'b' represents the bits
you want).

In this case you need only to mask the relevant bits with an And-mask,
that is, a sequence of n 1 bits, where n is the size of your data -- 3,
in this case:

aaaaabbb And 00000111 = 00000bbb

How to find the mask, you ask? Easy:

Dim Mask As Byte = (1 << Size) - 1

The first problem begins when the bits are not 'right-aligned' in the
byte:

aaabbbaa

To resolve this you must first put the bits in the 'right' position.
You do this by shifting the bits to right n positions, where n is the
leftmost position of the bit pattern plus one, minus the size of the
pattern:

Dim Shift As Byte = LeftmostBit + 1 - Size
Value = Value >> Shift

Notice, however, that the bit positions are numbered like this:

76543210

Therefore, in the previous example, the leftmost bit would be 4.

So, to put it all together so far you'd have something like:

<aircode>
Function GetBits(Value as Byte, _
LeftmostBit As Byte, _
Size as Byte) As Byte
Dim Shift As Byte = LeftmostBit + 1 - Size
Dim Mask As Byte = (1 << Size) - 1
Return (Value >> Shift) And Mask
End Function
</aircode>

Probably, depending on your settings, VB will give you all types of
warnings, because in the above expressions the value "1" is computed as
an integer, and the entire expression is promoted to integer. Getting
rid of the warnings remains as an exercise =))

Finally, things become *really* complicated when you have a stream of
values whose bits span two bytes:

aaaaaabb baaaaaaaa

To resolve this, you must treat the two bytes as a 16 bit value (a
Short, in VB). When you do this, everything else Just Works (TM), as
long as you treat the leftmost bit position as a number from 0 to 15
(and not 0 to 7, as in the previous example). If we use hexadecimal to
represent the bit positions, we'd have:

FEDCBA98 76543210

Therefore, in the above example, our leftmost bit would be 9 !

A function that would extract a given bit pattern from a sequence of
two bytes could be like this:

<aircode>
Function GetBits(FirstByte As Byte, _
ScndByte As Byte, _
LeftmostBit As Byte, _
Size As Byte) As Byte
Dim Value As Integer = (CInt(FirstByte) << 8) Or ScndByte
Dim Shift As Integer = LeftmostBit + 1 - Size
Dim Mask As Integer = (1 << Size) - 1
Dim Result As Integer = (Value >> Shift) And Mask
Return CByte(Result And 255)
End Function
</aircode>

HTH.

Regards,

Branco.


OK, you used the term "BitMask" which is beginning to jog my memory. I seem to
recall doing something similar as your example a few years ago back in VB6. I shall
have to look back through my archieves.

Fortunately, neither of the items I need from the header span two bytes.
One of the bytes holds two items: one item as bit 1 and the other item is the
remaining 7 bits.

The other byte also holds two items: the first 3 bits and the remaining 5 bits.

Thanks,

Gene
 
T

tomb

You could just declare a byte, then assign it a byte value that
corresponds to the bit positions, then use an AND to see what your
header value is. Example:
Suppose the value read from your header is thevalue
If you want to compare the first 3 bits
Dim theByte as Byte = 8
Dim theResult in Byte
A value of 8 is like saying in binary: 00000111

So, theByte = (thevalue AND theByte)
Now work with the resultant value of theByte

If you want to ignore the first bit and look at the next 7:
theByte = 254
That's like saying in binary 11111110
Then use your AND operator again

I think this is a lot easier than shifting bits around.

By the way, you would follow the same type of thing with 16 bit and 32
bit values, but they would be easier to work with in HEX. You could use
HEX with the Byte too - &h08 would still check the first 3 bits, &hfe
would compare those 7 bits.

T
 
B

Branco Medeiros

tomb wrote:
Suppose the value read from your header is thevalue
If you want to compare the first 3 bits
Dim theByte as Byte = 8
Dim theResult in Byte
A value of 8 is like saying in binary: 00000111
<snip>

Ops...

A value of 8 is like saying in binary 00001000
A value of *7* is 00000111

Regards,

Branco.
 
G

gene kelley

You could just declare a byte, then assign it a byte value that
corresponds to the bit positions, then use an AND to see what your
header value is. Example:
Suppose the value read from your header is thevalue
If you want to compare the first 3 bits
Dim theByte as Byte = 8
Dim theResult in Byte
A value of 8 is like saying in binary: 00000111

So, theByte = (thevalue AND theByte)
Now work with the resultant value of theByte

If you want to ignore the first bit and look at the next 7:
theByte = 254
That's like saying in binary 11111110
Then use your AND operator again

I think this is a lot easier than shifting bits around.

By the way, you would follow the same type of thing with 16 bit and 32
bit values, but they would be easier to work with in HEX. You could use
HEX with the Byte too - &h08 would still check the first 3 bits, &hfe
would compare those 7 bits.

T

Well, it should work, but after playing around with it for a couple of hours, I
re-read the fine print in the file spec. Apparently BigEndianUnicode is what is used
which, I think, means that I'll have to flip the byte order which "seems" to explain
the erroneous values I have been getting. The one saving grace here is that with
the test file, I know what the return values are supposed to be.

Gene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top