Login
`
Templates, Tools and Utilities
|
||
Add a comment to an Icetips ArticlePlease add your comments to this article.
Please note that you must provide both a name and a valid email address in order
for us to publish your comment. Comments are moderated and are not visible until they have been approved. Spam is never approved!
Back to article list Search Articles Add Comment Printer friendly Direct link Par2: Setting bits in a Byte 2011-08-18 -- Bob Roos variable = bor(variable,01h) ! turns on the right most bit
variable = band(variable,0FEh) ! turns it off
variable = bxor(variable,01h) ! flips it
Bruce Johnson adds:
x = BOR(x,SomeBit) ! turn it on
x = XBOR(x,SomeBit) ! flip it
x = BAND(x,255-SomeBit) ! turn it off
a couple of things I like to do here;
a) use an EQUATE to name the bit and
b) Use "binary" number syntax.
Consider for example the following;
Bit0 Equate(00000001b)
Bit1 Equate(00000010b)
Bit2 Equate(00000100b)
Bit3 Equate(00001000b)
Bit4 Equate(00010000b)
Bit5 Equate(00100000b)
Bit6 Equate(01000000b)
Bit7 Equate(10000000b)
BitMax Equate(11111111b)
using the 'b' numbering format it's easy to see where the bit falls in the
sequence.
And if say Bit3 and Bit4 always work together you can add an equate
ControlBits Equate(00011000b)
and so on.
this allows yor code to be quite readable - for example
x = Bor(x,Bit3+Bit6) ! turn on Bit 3 and Bit 6
x = Bxor(Bit2) ! flip bit 2
x = Band(BitMax-Bit7-Bit1) ! turn off bit 7 and bit 1
The only consideration you should really have to worry about is whether you
count the bits as
bit 1 to bit 8, or bit 0 to bit 7. That can go both ways, but usually I code
it to match the documentation
I'm working from. If it's my decision I'll use Bit 1 so it's consistent with
the rest of Clarion.
Today is November 21, 2024, 6:49 am This article has been viewed 35228 times.
|
|