Ewon TechForum
Providing technical solutions to technical requests


write a single BIT of integer
Chris Offline
Junior Member
#1
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
Reply

simon Offline
eWON Support
#2
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@
Reply

dblake Offline
Junior Member
#3
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
Reply



Possibly Related Threads…
10-04-2018, 11:13 AM
Last Post: simon



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy