Change one bit of a value (long)

Just starting out? Need help? Post your questions and find answers here.
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Change one bit of a value (long)

Post 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.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Re: Change one bit of a value (long)

Post 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:

Code: Select all

Macro Bchg(i,n)
  i!(1<<n)
EndMacro
Regards,
El_Choni
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Change one bit of a value (long)

Post by Joris »

That was fast.

Thank you El_Choni.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Change one bit of a value (long)

Post 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
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Re: Change one bit of a value (long)

Post 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
El_Choni
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Change one bit of a value (long)

Post 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.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Change one bit of a value (long)

Post 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)
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Change one bit of a value (long)

Post 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) :

Code: Select all

Debug Bin(BChg(a,bit1),#PB_Long)
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Post Reply