[Gambas-user] byte to bit & bit to byte

Jussi Lahtinen jussi.lahtinen at gmail.com
Sun Aug 14 00:40:03 CEST 2022


> I want to use bytes to indicate 8 possible groups in a team work in only
> one field of the table.
> Now, thanks to your answers, I can encode and decode the byte.
>

No need to do that.
Usually it is done like this (de facto standard in C):

Public Const Group1 As Byte = 1
Public Const Group2 As Byte = 2
Public Const Group3 As Byte = 4
Public Const Group4 As Byte = 8
Public Const Group5 As Byte = 16
Public Const Group6 As Byte = 32
 ...
'' IE values of 2^n, IE values presented with one bit.

Dim iByteHoldingGroups As Byte

'Here adding Groups 1, 3 and 6 to the variable iByteHoldingGroups one at
the time:
iByteHoldingGroups = iByteHoldingGroups OR Group1
iByteHoldingGroups = iByteHoldingGroups OR Group3
iByteHoldingGroups = iByteHoldingGroups OR Group6

'Or multiple at the same time:
iByteHoldingGroups = iByteHoldingGroups OR Group1 OR Group3 OR Group6

'Here is how you check what is in the variable iByteHoldingGroups:
If iByteHoldingGroups And Group1 Then Print "There is Group1 among the
groups."

'Or multiple at the same time:
If iByteHoldingGroups And (Group1 OR Group2) Then Print "At least one of
the asked groups is among the groups."

'Here is how you remove groups (here Group 1 and 3):
iByteHoldingGroups = iByteHoldingGroups - (Group1 OR Group3)


Jussi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220814/214b1a75/attachment.htm>


More information about the User mailing list