setting a bit
TC
Posts: 1,019
Hello all, I have been trying to find an easy way of setting one bit of a variable.
For example:
to read; Bit 15 = 0. To write, bit 15 = 1.
Thanks for your help
For example:
to read; Bit 15 = 0. To write, bit 15 = 1.
Thanks for your help

Comments
variable &= ! |< bitNumber ' this clears a bit
There are longer solutions when you want to change several bits, but these are the most compact single bit methods.
Thank You
Thank You
pub rd_bit(target, pos) '' returns bit value (0..1) of target.pos if ((pos => 0) and (pos =< 31)) return (target >> pos) & 1 else return 0 pub wr_bit(target, pos, value) '' writes value.0 to target.pos if (value & 1) return target | (1 << pos) else return target & !(1 << pos) pub toggle_bit(target, pos) '' toggles target.pos return target ^ (1 << pos)