Code archives/Algorithms/Bit Access
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This code has many uses, basically, it's 3 functions which allow you to read/write bits in a byte. This code could be expanded to write bits in integers, ect. No credit necessary, as this is basic programming. | |||||
Const BIT_0 = 1 Const BIT_1 = 2 Const BIT_2 = 4 Const BIT_3 = 8 Const BIT_4 = 16 Const BIT_5 = 32 Const BIT_6 = 64 Const BIT_7 = 128 Function SetBit(B:Byte Var, Bit:Int) B :| Bit End Function Function ClearBit(B:Byte Var, Bit:Int) B :- Bit End Function Function GetBit:Int(B:Byte, Bit:Int) Return B & Bit > 0 End Function ' - TEST CODE, DELETE THIS. Global Test:Byte = 0 SetBit(Test, BIT_0) SetBit(Test, BIT_2) SetBit(Test, BIT_4) SetBit(Test, BIT_6) ClearBit(Test, BIT_6) Print("BIT_0: " + GetBit(Test, BIT_0) ) Print("BIT_1: " + GetBit(Test, BIT_1)) Print("BIT_2: " + GetBit(Test, BIT_2)) Print("BIT_3: " + GetBit(Test, BIT_3)) Print("BIT_4: " + GetBit(Test, BIT_4) ) Print("BIT_5: " + GetBit(Test, BIT_5)) Print("BIT_6: " + GetBit(Test, BIT_6)) Print("BIT_7: " + GetBit(Test, BIT_7)) |
Comments
| ||
Won't this: Return B & Bit > 0 Return True or False if you just do this: Return B & Bit ??? |
| ||
yep. well, it'll return Bit or 0, but those will evaluate as True or False respectively in an If statement. |
| ||
Yes, I did that for debugging purpose, so it'd return 0 or 1. |
Code Archives Forum