Bit-field Structures

I have an unsigned 32-bit word that contains bit-field data. The following C/C++ structure is an example of one 32-bit word broken into bit-fields. In VB .NET, is it possible to define bit-fields within a structure (similar to the one below) so that the data can be accessed by "abc.data1", "abc.data2", etc.?

typedefstruct

{

UINT32 data1 : 10;

UINT32 data2 : 6;

UINT32 data3 : 14;

UINT32 data4 : 2 ;

} A_32_BIT_WORD_S;

[558 byte] By [VFaul] at [2008-1-10]
# 1

Forgive me, I am not real great with C, I only took one class in it a long time ago.

Assuming you just want to create a stucture made up of 4 unsigned integers, this should work as far as defining it

Code Snippet

Public Structure A_32_BIT_WORD_S

Dim data1 As UInt32

Dim data2 As UInt32

Dim data3 As UInt32

Dim data4 As UInt32

End Structure

I am guessing 10,6,14,and 2 are the values being assigned to each Uint? In VB you can't assign inital values to the structure until you create a variable instance. So you would do something like this:

Code Snippet

Private Sub DoSomething()

Dim MyWord As New A_32_BIT_WORD_S

With MyWord

.data1 = 10

.data2 = 6

.data3 = 14

.data4 = 2

End With

End Sub

kleinma at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

No...

I want "Dim abc as A_32_BIT_WORD_S" to be one 32-bit word. Then data1 is the first 10 bits of that word, data2 is the next 6 bits of the word, etc.

VFaul at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Hi,

The only way I can see of achieving this is to use BitWise AND operations within a CLASS like this.

The maxValue for a UInt32 is 4294967295 = ( 2 ^ 32 ) -1

Regards,

John.

Code Snippet

Public Class Form1

'The next highlighted line should be one line of code in your code window.>>

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim myWord As New custom32BitWord

myWord.data1 = UInt32.MaxValue

myWord.data2 = UInt32.MaxValue

myWord.data3 = UInt32.MaxValue

myWord.data4 = UInt32.MaxValue

'Due to bitwise masking with the AND function in the CLASS

'below then these values should add up to Uint32.MaxValue

Dim total As UInt32

total = myWord.data1 + myWord.data2 + myWord.data3 + myWord.data4

MessageBox.Show(total.ToString)

MessageBox.Show(UInt32.MaxValue.ToString)

End Sub

End Class

Public Class custom32BitWord

Private dayta1, dayta2, dayta3, dayta4 As UInt32

Public Property data1()

Get

Return dayta1

End Get

Set(ByVal value)

'1st 10 bits.

dayta1 = value And &HFFC00000 '= 11111111 11000000 00000000 00000000

End Set

End Property

Public Property data2()

Get

Return dayta2

End Get

Set(ByVal value)

'Next 6 bits.

dayta2 = value And &H3F0000 '= 00000000 00111111 00000000 00000000

End Set

End Property

Public Property data3()

Get

Return dayta3

End Get

Set(ByVal value)

'Next 14 bits.

dayta3 = value And &HFFFC '= 00000000 00000000 111111111 11111100

End Set

End Property

Public Property data4()

Get

Return dayta4

End Get

Set(ByVal value)

'Last 2 bits.

dayta4 = value And 3 '= 00000000 00000000 00000000 00000011

End Set

End Property

End Class

JohnOliver(UK)MSP,VSIP at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
No bit fields in .NET. But you can make it just look like it:

Module Module1
Public Structure A32BitWord
Public word As Integer
Public Property data1() As Integer
Get
Return ReadBits(word, 0, 10)
End Get
Set(ByVal value As Integer)
WriteBits(word, 0, 10, value)
End Set
End Property
' etc...
End Structure

Private Function ReadBits(ByVal word As Integer, ByVal offset As Integer, ByVal bits As Integer) As Integer
Dim mask As Integer = ((1 << bits) - 1) << offset
Return (word And mask) >> offset
End Function
Private Sub WriteBits(ByRef word As Integer, ByVal offset As Integer, ByVal bits As Integer, ByVal value As Integer)
Dim mask As Integer = ((1 << bits) - 1) << offset
value = (value << offset) And mask
word = (word And Not mask) Or value
End Sub
End Module

nobugz at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...