Howto convert a bitsfield to an int16

  • Thread starter Thread starter QuocSi
  • Start date Start date
Q

QuocSi

Hi all,

I have a bitsfield

Structure MyBits
a as boolean
b as boolean
c as boolean
d as boolean
end structure

I wish to convert it into an Int16 from the binary value of my structure.

Thanks for your help
 
Try this instead . . . There's maybe a slicker way to do this, but this may
get you out of trouble !

Class MyBits
Private i16 As BitArray

Public Sub New()
i16 = New BitArray(16, False)
End Sub

Default Public Property bits(ByVal bitIndex As Int16) As Boolean
Get
Return i16.Item(bitIndex)
End Get
Set(ByVal Value As Boolean)
i16.Set(bitIndex, Value)
End Set
End Property

Public ReadOnly Property int16Value() As Int16
Get
Dim val As Int16
Dim i As Int16
If i16(0) Then val = 1
For i = 1 To i16.Length - 1
If i16.Item(i) Then
val += 2 ^ (i - 1)
End If
Next
Return val

End Get
End Property

End Class
 
QuocSi,
As OHM suggests I would consider either a BitArray or a BitVector32.

http://msdn.microsoft.com/library/d.../frlrfsystemcollectionsbitarrayclasstopic.asp

http://msdn.microsoft.com/library/d...llectionsspecializedbitvector32classtopic.asp

OHM showed the BitArray, here is a BetVector32 sample:

Public Structure MyBits

Private Enum Bit
A
B
C
D
End Enum

Private m_value As BitVector32

Public Sub New(ByVal data As Int32)
m_value = New BitVector32(data)
End Sub

Public Property a() As Boolean
Get
Return m_value.Item(Bit.A)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bit.A) = value
End Set
End Property

Public Property b() As Boolean
Get
Return m_value.Item(Bit.B)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bit.B) = value
End Set
End Property

Public Property c() As Boolean
Get
Return m_value.Item(Bit.C)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bit.C) = value
End Set
End Property

Public Property d() As Boolean
Get
Return m_value.Item(Bit.D)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bit.D) = value
End Set
End Property

Public Function ToInt16() As Int16
Return CShort(m_value.Data)
End Function

Public Function ToInt32() As Int32
Return m_value.Data
End Function

Public Shared Function FromInt16(ByVal value As Int16) As MyBits
Return New MyBits(value)
End Function

Public Shared Function FromInt32(ByVal value As Int32) As MyBits
Return New MyBits(value)
End Function

End Structure


Hope this helps
Jay

| Hi all,
|
| I have a bitsfield
|
| Structure MyBits
| a as boolean
| b as boolean
| c as boolean
| d as boolean
| end structure
|
| I wish to convert it into an Int16 from the binary value of my structure.
|
| Thanks for your help
|
|
|
 
Doh!

I defined Enum Bit incorrectly, the function wants the mask for the bits,
not the index of said bits. Try the following Enum instead.

Private Enum Bit
A = 1 << 0
B = 1 << 1
C = 1 << 2
D = 1 << 3
End Enum

If you use VB.NET 2002 try:

Private Enum Bit
A = 1
B = 2
C = 4
D = 8
End Enum


Hope this helps
Jay

| QuocSi,
| As OHM suggests I would consider either a BitArray or a BitVector32.
|
|
http://msdn.microsoft.com/library/d.../frlrfsystemcollectionsbitarrayclasstopic.asp
|
|
http://msdn.microsoft.com/library/d...llectionsspecializedbitvector32classtopic.asp
|
| OHM showed the BitArray, here is a BetVector32 sample:
|
| Public Structure MyBits
|
| Private Enum Bit
| A
| B
| C
| D
| End Enum
|
| Private m_value As BitVector32
|
| Public Sub New(ByVal data As Int32)
| m_value = New BitVector32(data)
| End Sub
|
| Public Property a() As Boolean
| Get
| Return m_value.Item(Bit.A)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bit.A) = value
| End Set
| End Property
|
| Public Property b() As Boolean
| Get
| Return m_value.Item(Bit.B)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bit.B) = value
| End Set
| End Property
|
| Public Property c() As Boolean
| Get
| Return m_value.Item(Bit.C)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bit.C) = value
| End Set
| End Property
|
| Public Property d() As Boolean
| Get
| Return m_value.Item(Bit.D)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bit.D) = value
| End Set
| End Property
|
| Public Function ToInt16() As Int16
| Return CShort(m_value.Data)
| End Function
|
| Public Function ToInt32() As Int32
| Return m_value.Data
| End Function
|
| Public Shared Function FromInt16(ByVal value As Int16) As MyBits
| Return New MyBits(value)
| End Function
|
| Public Shared Function FromInt32(ByVal value As Int32) As MyBits
| Return New MyBits(value)
| End Function
|
| End Structure
|
|
| Hope this helps
| Jay
|
| || Hi all,
||
|| I have a bitsfield
||
|| Structure MyBits
|| a as boolean
|| b as boolean
|| c as boolean
|| d as boolean
|| end structure
||
|| I wish to convert it into an Int16 from the binary value of my structure.
||
|| Thanks for your help
||
||
||
|
|
 

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

Back
Top