254 of 318
ZBasic System Library 244 ZX Microcontroller Family SetBits Type Subroutine Invocation SetBits(target, mask, value) Parameter Method Type Description target ByRef Byte The byte to be modified. mask ByVal Byte The mask indicating which bits to modify. value ByVal Byte The value of the bits to store. Discussion This subroutine allows you to set the value 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 And Not mask) Or (value And mask) The mask parameter governs which bits will get updated. For each bit of the mask parameter that is a 1, the corresponding bit of the target will be set to the state of the corresponding bit of the value parameter. Bits of the target that correspond to zero bits of the mask parameter will remain unchanged. The advantage to using the SetBits() 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 SetBits() useful for modifying I/O ports and other Byte values in a multi-tasking environment. Example ' set the middle 4 bits of Port C to the binary value &B0110 Call SetBits(Register.PortC, &H3C, &H18) Compatibility This routine is not available in BasicX compatibility mode. Also, it is only supported by ZX firmware later than v1.0.0. See Also ToggleBits