I am attempting to set up a list whose elements are in the format of a Structure, but the Structure elements are simply a sequence of bits (for ultrafast bitflag checking), and there are 32 bits. So in other words, the list is composed of a series of 32-bit elements, meaning they are ultimately just a list of 4-byte sequences, to be parsed by code for their set bits.
I'd like to set up a Structure so i can use the code to quickly find the value of the bit im looking at and reduce the parsing code. Can i use EnumerationBinary within a Structure to define each bit, or does that have to be outside of the Structure and used by the bit-parsing code. I know i can just set up a single-byte element within the structure for each bit, but i expect there to be tens of thousands of elements in the list, and i'd like to reduce the amount of memory each boolean will occupy. In other words, i dont want to use 1 whole byte for a 1-bit piece of data — i'd rather use a parser on a compression of bits.
If i cant use binary enumerations inside a structure, what is the best way to set up a series of 32 bitflags in each line of a list?
I hope all this makes sense, but if you need more explanation, i can provide a clearer intention. I have not begun doing the code, but i am setting up my data structures, unsure of what i can and cant do with Structure design
Enumerations within structures?
-
- Addict
- Posts: 1572
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: Enumerations within structures?
No structure is needed for this.
Code: Select all
Variable.l = 1234
Debug RSet(Bin(Variable), 32, "0")
If Variable ; One of the bits is set in the variable.
For i=0 To 31
If Variable & (1<<i)
Debug "Set bit "+i
EndIf
Next
EndIf
Re: Enumerations within structures?
PureBasic doesn't have a bit sized type, so you can't build a structure the way you describe. But, as User_Russian already says, you don't need one to do this. If you want up to 32 flags, a long will be sufficient and the EnumerationBinary can describe your flags correctly.
There is a 'gotcha' that you should be aware of though, if you aren't already. In a long the 32nd bit represents the sign so a greater than zero general test like this won't work when this bit is set:
You can however do this instead:
There is a 'gotcha' that you should be aware of though, if you aren't already. In a long the 32nd bit represents the sign so a greater than zero general test like this won't work when this bit is set:
Code: Select all
Flags.l = 1 << 31
Debug Flags
If Flags > 0
Debug "#True"
Else
Debug "#False"
EndIf
Code: Select all
Flags.l = 1 << 31
Debug Flags
If Flags
Debug "#True"
Else
Debug "#False"
EndIf
Re: Enumerations within structures?
Change name of bits ...
Code: Select all
;
EnumerationBinary
#bit_0
#bit_1
#bit_2
#bit_3
#bit_4
#bit_5
#bit_6
#bit_7
#bit_8
#bit_9
#bit_10
#bit_11
#bit_12
#bit_13
#bit_14
#bit_15
#bit_16
#bit_17
#bit_18
#bit_19
#bit_20
#bit_21
#bit_22
#bit_23
#bit_24
#bit_25
#bit_26
#bit_27
#bit_28
#bit_29
#bit_30
#bit_31
EndEnumeration
Macro SetBit(Value, Bit)
Value = Value | (Bit)
EndMacro
Macro ClrBit(Value, Bit)
Value = value & ~(Bit)
EndMacro
Define flag.l
SetBit(flag, #bit_2)
Debug flag
If flag & #bit_2
Debug "bit 2"
EndIf
ClrBit(flag, #bit_2)
Debug flag
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Enumerations within structures?
Ok thanks for all your advice.
The reason i wanted to use a structure as my list element is so i can quickly access a bit by variable name. But as i already knew, there arent one-bit variable sizes, the smallest is one byte.
So think i'll incorporate @mk-soft's code and just store the list element as 4 bytes worth of bits for each element. It's compact and parsing the bitflags will be relatively fast rather than saving booleans as variables.
The reason i wanted to use a structure as my list element is so i can quickly access a bit by variable name. But as i already knew, there arent one-bit variable sizes, the smallest is one byte.
So think i'll incorporate @mk-soft's code and just store the list element as 4 bytes worth of bits for each element. It's compact and parsing the bitflags will be relatively fast rather than saving booleans as variables.