TYPE - bitfields

Forum for discussion about the documentation project.
Post Reply
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

TYPE - bitfields

Post by ppf »

Hi fxm,

I am using offline FB manual 1.05 html.
Just founded on page "Type" this :

"Description - Type is used to declare custom data types containing ... bitfields, ...

so I am interested in this syntax:

"fieldname : bits As DataType [= initializer]" and
"As DataType fieldname : bits [= initializer], ..."

Could you provide an example to understanding that and for test a computing speed of array of bitfields ?

Thank you
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: TYPE - bitfields

Post by fxm »

For example:

Code: Select all

Union bitFromUbyte
  ub As Ubyte
  Type
    ub0 : 1 As Ubyte
    ub1 : 1 As Ubyte
    ub2 : 1 As Ubyte
    ub3 : 1 As Ubyte
    ub4 : 1 As Ubyte
    ub5 : 1 As Ubyte
    ub6 : 1 As Ubyte
    ub7 : 1 As Ubyte
  End Type
End Union

Dim As bitFromUbyte bits
With bits
  .ub = &b10100110
  Print Bin(.ub, 8)
  Print .ub7 & .ub6 & .ub5 & .ub4 & .ub3 & .ub2 & .ub1 & .ub0
  Print
  Swap .ub7, .ub0
  Swap .ub6, .ub1
  Swap .ub5, .ub2
  Swap .ub4, .ub3
  Print Bin(.ub, 8)
  Print .ub7 & .ub6 & .ub5 & .ub4 & .ub3 & .ub2 & .ub1 & .ub0
  Print
  .ub7 = Not .ub7
  .ub6 = Not .ub6
  .ub5 = Not .ub5
  .ub4 = Not .ub4
  .ub3 = Not .ub3
  .ub2 = Not .ub2
  .ub1 = Not .ub1
  .ub0 = Not .ub0
  Print Bin(.ub, 8)
  Print .ub7 & .ub6 & .ub5 & .ub4 & .ub3 & .ub2 & .ub1 & .ub0
End With

Sleep

Code: Select all

10100110
10100110

01100101
01100101

10011010
10011010
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: TYPE - bitfields

Post by fxm »

By defining a get/set property, one can access the different bits through an index:
Equivalent code to the one above:

Code: Select all

Union bitFromUbyte
  Declare Property b (Byval index As Uinteger, Byval value As Ubyte)
  Declare Property b (Byval index As Uinteger) As Ubyte
  ub As Ubyte
  Type
    ub0 : 1 As Ubyte
    ub1 : 1 As Ubyte
    ub2 : 1 As Ubyte
    ub3 : 1 As Ubyte
    ub4 : 1 As Ubyte
    ub5 : 1 As Ubyte
    ub6 : 1 As Ubyte
    ub7 : 1 As Ubyte
  End Type
End Union

Property bitFromUbyte.b (Byval index As Uinteger, Byval value As Ubyte)
  Select Case As Const index
  Case 0
    This.ub0 = value
  Case 1
    This.ub1 = value
  Case 2
    This.ub2 = value
  Case 3
    This.ub3 = value
  Case 4
    This.ub4 = value
  Case 5
    This.ub5 = value
  Case 6
    This.ub6 = value
  Case 7
    This.ub7 = value
  End Select
End Property

Property bitFromUbyte.b (Byval index As Uinteger) As Ubyte
  Select Case As Const index
  Case 0
    Return This.ub0
  Case 1
    Return This.ub1
  Case 2
    Return This.ub2
  Case 3
    Return This.ub3
  Case 4
    Return This.ub4
  Case 5
    Return This.ub5
  Case 6
    Return This.ub6
  Case 7
    Return This.ub7
  End Select
End Property

Dim As bitFromUbyte bits
With bits
  .ub = &b10100110
  Print Bin(.ub, 8)
  For I As Integer = 7 To 0 Step -1
    Print .b(I);
  Next I
  Print
  Print
  For I As Integer = 7 To 4 Step -1
    Dim As Ubyte b = .b(I)
    .b(I) = .b(7-I)
    .b(7-I) = b
  Next I
  Print Bin(.ub, 8)
  For I As Integer = 7 To 0 Step -1
    Print .b(I);
  Next I
  Print
  Print
  For I As Integer = 7 To 0 Step -1
    .b(I) = Not .b(I)
  Next I
  Print Bin(.ub, 8)
  For I As Integer = 7 To 0 Step -1
    Print .b(I);
  Next I
  Print
End With

Sleep

Code: Select all

10100110
10100110

01100101
01100101

10011010
10011010
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: TYPE - bitfields

Post by speedfixer »

Simple example - only to show how it works - nothing extra.

Code: Select all

union bits
    all as ulong
    type
            a:1 as ubyte
            b:2 as ubyte
            c:2 as ubyte
            d:3 as ubyte
    end type
end union

dim as bits master

master.a = 1
master.b = 2
master.c = 3
master.d = 4

cls
print : print "  assign bits: "

print " a: "; master.a; "  (1 bit)"
print " b: "; master.b; "  (2 bits)"
print " c: "; master.c; "  (2 bits)"
print " d: "; master.d; "  (3 bits)"
print
print " bits: "; bin(master.all); "  (total 8 bits)"
print " master.all: "; master.all
print : print "    assign master.all: "

master.all = 61680                 ' will use same bits as above

print " master.all: "; master.all
print " bits: "; bin(master.all); "   (32 bits, only lowest 16 needed, first 16 all '0')"

print : print "  bits result: "

print " a: "; master.a; "  (1 bit)"
print " b: "; master.b; "  (2 bits)"
print " c: "; master.c; "  (2 bits)"
print " d: "; master.d; "  (3 bits)"

print : print "   assign master.all: "

master.all = 256                ' just out of the bits range
print " master.all: "; master.all
print " bits: "; bin(master.all); "   (32 bits, last 16 all '0')"

print : print "    bits result: "
print " bits: "; bin(master.all)
print " a: "; master.a; "  (1 bit)"
print " b: "; master.b; "  (2 bits)"
print " c: "; master.c; "  (2 bits)"
print " d: "; master.d; "  (3 bits)"

print : print "   assign master.all: "

master.all = 255                ' just out of the bits range
print " master.all: "; master.all
print " bits: "; bin(master.all); "   (32 bits, last 16 all '1')"

print : print "    bits result: "
print " bits: "; bin(master.all)
print " a: "; master.a; "  (1 bit)"
print " b: "; master.b; "  (2 bits)"
print " c: "; master.c; "  (2 bits)"
print " d: "; master.d; "  (3 bits)"
Post Reply