Switch to desktop version  
write a single BIT of integer - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: BASIC Script (https://techforum.ewon.biz/forum-52.html)
+--- Thread: write a single BIT of integer (/thread-528.html)



write a single BIT of integer - Chris - 21-02-2018

Hi,


how does it work to write a single bit of a Integer?

Read works like this:
DO1@=MW1@#3  // DO1 = bit 3 of MW1

But write does NOT work like this:
MW1@#10=DI1@  //bit 10 of MW1 = DI1

Thanks for help
Best regards Chris



RE: write a single BIT of integer - simon - 21-02-2018

Hi,

I have tested with two MEM tags :

MEM0@ = 123
MEM1@ = 1
MEM0@#2 = MEM1@
PRINT MEM0@

and it works good.

By the way, to set or reset a bit (as a workaround), you just need to add (to set) or decrease (to reset) the value by 2^bitNbr

MEM0@ = 123
BitNbr% = 2
MEM0@ = MEM0@ + 2^BitNbr%
PRINT MEM0@


RE: write a single BIT of integer - dblake - 14-05-2018

Using addition to set a bit or subtraction to clear it is dangerous.  If the bit were already set when you tried to set it, you would not only clear the bit, but you would cause a carry over into the next bit.

Here are the safe methods to manipulate a bit:

MEM0@ = 123
BitNbr% = 2
MEM0@ = MEM0@ OR 2^BitNbr%                // Set a particular bit
MEM0@ = MEM0@ AND (BNOT 2^BitNbr%)  // Clear a particular bit
MEM0@ = MEM0@ XOR 2^BitNbr%              // Toggle a particular bit