Page 1 of 1
Bit operators
Posted: Fri Oct 03, 2003 11:54 am
by einander
Thanks Fred for PB!
Please: is there any chance to add commands to set, clear, test and change single bits on an integer?
Something to replace
Code: Select all
Procedure BitSet(A,N) ;Sets the N bit
ProcedureReturn A | 1 << N
EndProcedure
Procedure BitClr(A,N) ; Clears the N bit
ProcedureReturn BitSet(A ,N) ! BitSet(0,N)
EndProcedure
Procedure BitTst(A,N) ;Returns state of N bit
If A & BitClr(A,N)=A : ProcedureReturn #FALSE : EndIf
ProcedureReturn #TRUE
EndProcedure
Procedure BitChg(A,N) ;Change state of N bit
If A & BitClr(A,N)=A : ProcedureReturn A|1<<N: EndIf
ProcedureReturn BitSet(A,N) ! BitSet(0,N)
EndProcedure
;____________Test_____________________
A=BitSet(A,4) : Debug Bin(a)
A=BitSet(BitSet(A,5),6) : Debug Bin(A)
A=BitChg(A,5) : Debug Bin(A)
A=BitChg(A,5) : Debug Bin(A)
Debug BitTst(A,4)
[/code]
Posted: Fri Oct 03, 2003 12:07 pm
by LarsG
Maybe use Assembler commands?!?
-Lars
Posted: Fri Oct 03, 2003 2:07 pm
by Froggerprogger
btw:
Here are 2 procedures to Peek/Poke single bits inside large memoryblocks.
Further there are 2 procedures to Peek/Poke the value of 1-31 Bits inside large memoryblocks.
This is useful for example to store a lot of LONG-values which do not exactly use all 32 Bits in only the needed Bits/value.
Moved to
>here: Tricks'nTips<
Posted: Fri Oct 03, 2003 6:49 pm
by Psychophanta
Einander, besides of the Froggerprogger functions, you can implement a more fast and nice procedures than you show.
You have these i386 assembler instructions to do that (very descriptive instructions, without use of other logical operations):
BT ;Bit Test
BTS ;Bit Test and Set
BTR ;Bit Test and Reset
BTC ;Bit Test and Complement (Change)
The only thing is that you must take care because as second operand (the offset) is only allowed a general register or a inmediate value.
As you see, and as said LarsG, your request is almost implemented in PB, cause assembler interface.
AL
Posted: Fri Oct 03, 2003 6:58 pm
by blueznl
i'm totally non-assembly on 8080ish machines, could you give an example with variables?
Posted: Fri Oct 03, 2003 11:51 pm
by einander
[quote]As you see, and as said LarsG, your request is almost implemented in PB [/quote]
Thanx Psychophanta!
I'm tryng to understand how to do it in assembler. No luck...
As PB is growing quickly, I hope "almost implemented" turns soon into "full implemented"!
Greetings from Spain
Einander
Posted: Sat Oct 04, 2003 2:44 pm
by Psychophanta
In my opinion, its preferable to have operators instead of functions in PB; because the language will become more easely readable, and concise.
It is preferable to eliminate functions from PB if posible to expresate it using single character operators.
You can use this if you want:
Code: Select all
var=%101001011110
BTS [v_var],4;<--BitSet
Debug Bin(var)
var=%101001011110
BTR [v_var],4;<--BitClr
Debug Bin(var)
var=%101001011110
tst.b
BT [v_var],4;<--BitTst
SETC byte[v_tst]
Debug Bin(tst)
var=%101001011110
BTC [v_var],4;<--BitChg
Debug Bin(var)
However, i think it is more wise and i prefer to use AND (& operator), OR (| operator), NOT (~ operator) and XOR (! operator) for to manage and check bits inside variables, constants or expresions.
Thank you!
Posted: Sun Oct 05, 2003 10:04 pm
by einander
Psychophanta:
Thanx! I'm looking on the assembler help to figure how to use your instructions.
On first try, PB 3.79.a gives me Syntax Error on BT, BTS, BTR, BTC.
Einander
PB registered user
Posted: Sun Oct 05, 2003 11:17 pm
by Saboteur
Have you enabled INLINE ASSEMBLER in editor?
Posted: Mon Oct 06, 2003 12:27 am
by einander
[quote=]Have you enabled INLINE ASSEMBLER in editor?[/quote]
NO!!!
Thanx Saboteur! Now it's OK!
Einander