![]() |
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 ![]() Just add these functions anywhere : Code: Public Function ToggleBit(ByRef initialByte As Byte, ByVal bytLocation As Byte) As Byte 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 Now enjoy ![]() ![]() |