473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading value of a bit?

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

Nov 13 '06 #1
7 6566
Gene,

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

Cor

"gene kelley" <ok**@by.meschreef in bericht
news:c1********************************@4ax.com...
>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

Nov 14 '06 #2
gene kelley wrote:
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.

Nov 14 '06 #3
On 13 Nov 2006 22:20:58 -0800, "Branco Medeiros" <br*************@gmail.comwrote:
>gene kelley wrote:
>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.

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

Nov 14 '06 #4
Check out the code snippets below. There are several functions that may
answer your need for conversion to binary

http://www.freevbcode.com/ShowCode.Asp?ID=8271

Tom

"gene kelley" <ok**@by.mewrote in message
news:c1********************************@4ax.com...
>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

Nov 14 '06 #5
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

gene kelley wrote:
>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
Nov 14 '06 #6

tomb wrote:
<snip>
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.

Nov 14 '06 #7
On Tue, 14 Nov 2006 10:54:12 -0500, tomb <to**@technetcenter.comwrote:
>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
>
gene kelley wrote:
>>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
Nov 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
3
by: Carl Lindmark | last post by:
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I believe the question is better suited in this XML group* Hello all, I'm having some problems understanding all the ins and...
3
by: roaher | last post by:
hi I have some trouble reading this macro: #define SMC_inl(r) (*((volatile dword *)(SMC_BASE_ADDRESS+(r)))) also consider that SMC_BASE_ADDRESS is address base of I/O mapped peripherial...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
4
by: Fred West | last post by:
I have a class with a private Int32 data member that gets modified from multiple background threads. To synchronize these modifications I use the lock statement: Int32 count = 0; object...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
9
by: Mike Reed | last post by:
I must be having a "senile" day! I cannot recall, nor get to work, code to read a cookie's expiration date/time in an ASP page/VBScript. What am I missing? *** Sent via Developersdex...
2
by: sri | last post by:
I think the following question is related to this group that is why I am posting The question is: I am reading bytes from binary file. At a particular position the byte value is "FF". I am...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.