Mirage Engine
Bit Manipulation - Printable Version

+- Mirage Engine (https://mirage-engine.uk/forums)
+-- Forum: Mirage Source (Nostalgia) (https://mirage-engine.uk/forums/forumdisplay.php?fid=61)
+--- Forum: Archive (2006-2011) (https://mirage-engine.uk/forums/forumdisplay.php?fid=18)
+---- Forum: Resources (https://mirage-engine.uk/forums/forumdisplay.php?fid=49)
+---- Thread: Bit Manipulation (/showthread.php?tid=1300)



Bit Manipulation - JokeofWeek - 20-09-2007

This is an idea that I had gotten a while ago from Dave's tutorial for bitfields, however I cannot find it anymore and I find it definitely useful, as it can save loads of bandwith overtime, so I decided that I should remake it, however, give Dave all credit for this one Smile. What it is is basically, it manipulates the actual bits of a byte, and can, potentially, let you store 8 boolean values in one byte, which is great when you are storing things like flags.

Just add these functions anywhere :

Code:
Public Function ToggleBit(ByRef initialByte As Byte, ByVal bytLocation As Byte) As Byte
initialByte = initialByte Xor (2 ^ bytLocation)
End Function
Public Function GetBitValue(ByVal initialByte As Byte, ByVal bytLocation As Byte) As Byte
If (initialByte And (2 ^ bytLocation))  0 Then
    GetBitValue = 1
Else
    GetBitValue = 0
End If
End Function
Public Function ClearByte(ByRef initialByte As Byte)
    initialByte = 0
End Function

ToggleBit Function :
Usage : Call ToggleBit(byteVariable,bitNumber)
What this does is just toggle a certain bit. If it is set to 1, it will go back to 0, and if it is set to 0, it will go to 1. As you know, binary has 8 bits (00000000) and each of these can be set to either 1 or 0. To use this, just put the byte you are using as a bitfield in the first parameter, and for the bitnumber, put the bit you want to use (1-8). Just note that this is using your bitfield with a ByRef parameter instead of a ByVal, which means it will alter it in memory, so you don't have to do something like :

Code:
testField = ToggleBit(testField,1)

You can just do this :

Code:
Call ToggleBit(testField,1)



GetBitValue Function :
Usage : variable = GetBitValue(testField,1)
What this function does is it returns the value of a certain bit in a field, it will return 1 if it is set to 1, else it will set 0. Just remember that it returns a byte, so you have to assign it to a variable, as shown in the Usage.



ClearBit Function :
Usage : Call ClearBit(testField)
What this function does is just sets your bitfield back to 0. Once again, this is a byRef command, so you just have to call it like this :

Code:
Call ClearBit(testField)

And it will clear it, no need to assign it to another variable.




[b]- Example Code -[b]
This is just an example code to show the functionos :

Code:
' Our bitfield
Dim testField As Byte

' A variable to hold the value of abit
Dim bitValue As Byte

' This sets the first bit to 1
ToggleBit testField, 1

' This sets the third bit to 1
ToggleBit testField, 3

' This sets bitValue to the value of the first bit
bitValue = GetBitValue(testField, 1)

' This sets the first bit back to 0
ToggleBit testField, 1

' This clears the bitfield
ClearByte testField

Now enjoy Smile And once again, thanks Dave for this, not me Smile Although you might want to be careful when using this, as if it you set the byte to a character and it is sent in a packet, and it is either the END_CHAR or SEP_CHAR, it could mess up your packets.