I am trying to read a byte from a certain address in a file. But, I am trying to read the byte in bit format (bits 0-7). I have tried to use the BitConverter and the BitArray classes but I cannot figure it out.
I am pretty sure that BitArray is the one I need but there are no methods that look like they would do anything I want. Maybe I am just missing it. I checked the help section but the examples there were not what I am trying to do.
Any help would be greatly appreciated.

Trouble reading bits.
jreed
Remember I was saying were going to demonstrate how to set bit 3.
in binary, setting bit 3 would look like 00001000 = 8.
ByteEntity means any byte
If Mask = 8, bit three is set. Mask Logically ORed with any byte or byteenity would insure that bit 3 is set.
8. Logically ORed with any byte will insure that bit three is set.
TravisQuerec
Dim buffer(bytecount) As Byte
Dim word As String
For i As Byte = 0 To 7
If TestByteBits(67248, buffer, i) Then word = "" Else word = "not "
MsgBox("With a decimal value of " & buffer(67248) & " in byte number " & 67248 & vbCrLf & _
"bit number " & i & " is " & word & "set.", MsgBoxStyle.Information, word & "set")
Next
End Sub
Private Function TestByteBits(ByVal Offset As Integer, ByRef Buffer As Byte(), ByVal BitNo As Integer) As Boolean
'if Bitno exceeds 7 - declare the caller has made an error. (for you to impliment)
Dim mask As Byte = 1 << BitNo
If ((Buffer(Offset) And mask) <> 0) Then Return True
Return False
End Function
Ok so that it what I used of your code. but for some reason I says that the deciaml value is 0 when it is not. Maybe it's not using the buffer All my other function display all their values correctly from the buffer. Also thanks for adding in the MsgBox. It made it so much easier to understand.
Sohailsoh
If cSkill_3.Checked = True Then Buffer.SetByte(b, 67247, 1 << 2)
If cSkill_6.Checked = True Then Buffer.SetByte(b, 67247, 1 << 5)
WriteBytes(b, tbCurrentFile.Text)
I went through the help files and found out what << was used for. The above code is probably completely inefficient, though. Now my problem is even if both the statements are true (both boxes checked) It only changes bit 6. So, I get a value of 20h instead of 24h. I figure I need to maybe add the two statements together somehow, although I have not figured out how to accomplish such a task. Those boolean operators give me a headache. Any ideas
Mario Chenier - Microsoft
I wouldn't do it that way....
I'd copy the byte out of memory and play with it
If I modify it and want to put it back then copy the byte back to memory as a byte.
"<>" is a boolean it mean "Not equals to"
the &H1 is called a constant it can be declared in a number of ways
const mybyte as byte = &h22B
makes a byte constant Note the "B" after the quotation
But anyway... here's is something to play with...
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim buffer(300) As Byte
Dim b As Boolean = TestByteBits(5, buffer, 1, 0)
End Sub
Private Function TestByteBits(ByVal Offset As Integer, ByRef Buffer As Byte(), _
ByVal Byteno As Integer, ByVal BitNo As Integer) As Boolean
'if Bitno exceeds 7 - declare the caller has made an error. (for you to impliment)
Dim mask As Byte = 1 << BitNo
If Buffer(Offset) And mask <> 0 Then Return True
Return False
End Function
End Class
Matthew Petersen
In your example, I can't see where bytecount is defined......
and if this code:
MsgBox("With a decimal value of " & buffer(67248) & " in byte .......
says the value is zero...... I'd count on it being zero.
apedrosa
It pretty clear why it doesn't work
I've also recommended what you should be doing in relation to memory. You've chose not to follow those recommendations but you've made errors that expeience has taugh me can happened.
First of the << and >> operators DO NOT set bits. they Shift all the bits in a byte or short or integer.
So what you did was a guranteed memory corruptor.
How do you set a bit in byte
lets say you want to set bit 3 in a byte.
Mask = 8 ' Mask is a byte
if you have a known mask you can Logically OR the mask to the byte
ByteEntity= ByteEntity OR Mask
Bit 3 has been set
Scott Weinstein
I got it set to display the output in a textbox.
And I don't understand all of the arguments you used. Offset is the address and Buffer is the buffer and bitno is the bit but what would byteno be
Does If Buffer(Offset) And mask <> 0 mean if the offset and the mask are both equal to zero
This seems alot harder than it should be. Or maybe I am just not understanding it.
BlackZombie
For i As Byte = 0 To 7
If TestByteBits(67247, b, 0) Then cSkill_1.Checked = True Else cSkill_1.Checked = False
If TestByteBits(67247, b, 1) Then cSkill_2.Checked = True Else cSkill_2.Checked = False
If TestByteBits(67247, b, 2) Then cSkill_3.Checked = True Else cSkill_3.Checked = False
If TestByteBits(67247, b, 3) Then cSkill_4.Checked = True Else cSkill_4.Checked = False
If TestByteBits(67247, b, 4) Then cSkill_5.Checked = True Else cSkill_5.Checked = False
If TestByteBits(67247, b, 5) Then cSkill_6.Checked = True Else cSkill_6.Checked = False
Next
(I only need the first 6 bits)
I got it to work though and am currently trying to figure out how to write the bits back to the buffer.
Thanks.
jbog91
Well that’s what I get for writing code at 4:00 AM. <blush>.
There was an extra unused argument… AND there was an error – not in the logic but there was a precidence error in the IF statement. I didn’t change my logic, I just added some parentheses. This does work, Troy.
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim buffer(300) As Byte
Const offset As Integer = 5
Dim word As String
buffer(offset) = 23
For i As Byte = 0 To 7
If TestByteBits(offset, buffer, i) Then word = "" Else word = "not"
MsgBox("With a decimal value of " & buffer(offset) & " in byte number " & offset & vbCrLf & _
"bit number " & i & " is " & word & " set.", MsgBoxStyle.Information, word & " set")
Next
End Sub
Private Function TestByteBits(ByVal Offset As Integer, ByRef Buffer As Byte(), ByVal BitNo As Integer) As Boolean
'if Bitno exceeds 7 - declare the caller has made an error. (for you to impliment)
Dim mask As Byte = 1 << BitNo
If ((Buffer(Offset) And mask) <> 0) Then Return True
Return False
End Function
End Class
Does If Buffer(Offset) And mask <> 0 mean if the offset and the mask are both equal to zero
No, it doesn't. It means if the result of the Logical AND of the byte and the Mask is non-zero then the bit is set. A Logical AND sets bits that are set in both entities, so if bit 1 of both the mask and the byte were set, their Logical AND would be non-zero.
Good luck.
xiaoyifang
So how am I corrupting memory And, which method should I use if not this one
I figured if I used the shift operator to extract the bits then I could use it to insert them as well.
Hmm...This is pretty much the only part of my program that does not work.
amiller099
Now here are my novice questions:
Why does Mask = 8.
What is ByteEntity
Which part of ByteEntity = ByteEntity OR Mask represents bit 3
Wait, wait, wait...Let me see if I can explain what I am trying to do. I have 8 checkboxes. I want each one to represent a different bit (0-7). When I check box1 then I want bit 0 to equal true, and so on. If I could have this process explained to me then my question will be answered.
Remember, I don't know every expression or method in BASIC, or even most of them. If I did then I wouldn't be here.
Thanks.
JustBitsNBytes
If (CDec(b(67248)) And &H1 <> 1) Then
cSkill_1.Checked = True
End If
Here are some things that I don't understand. My code editor says that &H1 is an integer, but what is it exactly It says that <> is boolean, but I have never seen that symbol in my searches, so what does it mean Also would the above example mean: if bit 1 at address 67248 is true, or am I totally off I think &H mean it's hex, right Ah, I am confused. I want if a certain bit is true then to check a checkbox. So, am I right Thanks again.
Gertus
No.
You're not setting a bit. You're corrupting memory that way.
Steve Landis
Read the byte as a byte and then inspect individual bits
Here's how to test bit 0
if byte and &h1 <> 0 then
endif
Remember troy... just read and write bytes to a bytes array.
Do your other processing in the interim.