Page 326 of 346 Start Back Next End ZBasic System Library 314 ZBasic Microcontrollers ToggleBits Type Subroutine Invocation ToggleBits(target, mask) Parameter Method Type Description target ByRef Byte The byte to be modified. mask ByVal Byte The mask indicating which bits to modify. Discussion This subroutine allows you to change the state of one or more bits in a byte while leaving others unchanged. Effectively, the result is the same as using the statement below. target = target Xor mask The mask parameter governs which bits will get changed. For each bit of the mask parameter that is a 1, the corresponding bit of the target will be set to the opposite of its current state. Bits of the target that correspond to zero bits of the mask parameter will remain unchanged. The advantage to using the ToggleBits() subroutine instead of the equivalent statement is twofold. Firstly, it is more efficient, resulting in less code and faster execution time. Secondly, and perhaps more importantly, it performs the action as an atomic operation, i.e. one that is guaranteed, once begun, to complete without an intervening task switch. This characteristic makes ToggleBits() useful for modifying I/O ports and other Byte values in a multi-tasking environment. Example ' change the state of the two least significant bits of Port C Call ToggleBits(Register.PortC, &H03) Compatibility This routine is not available in BasicX compatibility mode. Also, it is only supported by ZX VM firmware later than v1.0.0. See Also SetBits