Page 1 of 1
Change one bit of a value (long)
Posted: Fri Apr 19, 2013 12:06 pm
by Joris
Hi,
I try to find a replacement for the GFA 'Bchg' command. It does change one bit of the value at a given position.
a.l=7
a=Bchg(a,3) ;should return 3
a=Bchg(a,3) ;should return back 7
I found an (old) macro here but it doesn't work (and I think it's not the correct setup neither).
Code: Select all
Macro Bchg(i,n)
Int(Pow(i,1 << n))
EndMacro
In my opinion it should be more something like the one below, but also that doesn't work.
Code: Select all
Procedure Bchg(i,n)
ProcedureReturn i ~(1 << n) ; i NOT(1 << n)
EndProcedure
So how to ?
Thanks.
Re: Change one bit of a value (long)
Posted: Fri Apr 19, 2013 12:20 pm
by El_Choni
This works here:
Code: Select all
Macro Bchg(i,n)
i!(1<<(n-1))
EndMacro
If the n index was 0 based (which would be the standard), the macro could be a bit shorter:
Regards,
Re: Change one bit of a value (long)
Posted: Fri Apr 19, 2013 12:28 pm
by Joris
That was fast.
Thank you El_Choni.
Re: Change one bit of a value (long)
Posted: Fri Apr 19, 2013 12:40 pm
by Joris
Ooops too fast...
Your code result is wrong El_Choni.
Code: Select all
Macro Bchg(i,n)
i!(1<<n-1)
EndMacro
a.l=7
Debug a
Debug Bin(a)
a=Bchg(a,8)
Debug Bin(a)
Result in this :
7
111
11111000
The ! character should be replaced by a ~ character I suppose, but I can't get it done without getting an error.
While it has to become this :
7
111
100000111
Re: Change one bit of a value (long)
Posted: Fri Apr 19, 2013 12:55 pm
by El_Choni
You've changed the code I posted. The parentheses are needed
Code: Select all
Macro Bchg(i,n)
i!(1<<(n-1))
EndMacro
Re: Change one bit of a value (long)
Posted: Fri Apr 19, 2013 1:05 pm
by Joris
El_Choni wrote:You've changed the code I posted. The parentheses are needed
Code: Select all
Macro Bchg(i,n)
i!(1<<(n-1))
EndMacro
Damned, sorry El_Choni... being to busy checking things, changing things, deleting things.
(I'm gonna save it now before going on.
Again thanks.
Re: Change one bit of a value (long)
Posted: Fri Apr 19, 2013 4:29 pm
by Michael Vogel
GFA had some more binary functions, hopefully these marcos are working without troubles:
Code: Select all
Macro BChg(value,bit)
(value!(1<<(bit-1)))
EndMacro
Macro BSet(value,bit)
(value|(1<<(bit-1)))
EndMacro
Macro BClr(value,bit)
(value&(~(1<<(bit-1))))
EndMacro
Macro BTst(value,bit)
(value>>(bit-1)&1)
EndMacro
a=%00001111
bit1=3
bit2=6
Debug RSet(Bin(BChg(a,bit1)),8,"0")
Debug RSet(Bin(BChg(a,bit2)),8,"0")
Debug RSet(Bin(BSet(a,bit1)),8,"0")
Debug RSet(Bin(BSet(a,bit2)),8,"0")
Debug RSet(Bin(BClr(a,bit1)),8,"0")
Debug RSet(Bin(BClr(a,bit2)),8,"0")
Debug BTst(a,bit1)
Debug BTst(a,bit2)
Re: Change one bit of a value (long)
Posted: Fri Apr 19, 2013 10:39 pm
by Joris
Michael as you put it here like this :
Code: Select all
Debug RSet(Bin(BChg(a,bit1)),8,"0")
I found this (just a hint) :