Bit operators

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Bit operators

Post 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]
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Maybe use Assembler commands?!?

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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<
Last edited by Froggerprogger on Fri Oct 03, 2003 7:18 pm, edited 1 time in total.
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

AL
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i'm totally non-assembly on 8080ish machines, could you give an example with variables?
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post 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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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!
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post 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
Saboteur
Enthusiast
Enthusiast
Posts: 273
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Post by Saboteur »

Have you enabled INLINE ASSEMBLER in editor?
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

[quote=]Have you enabled INLINE ASSEMBLER in editor?[/quote]

NO!!! :oops:
Thanx Saboteur! Now it's OK!
Einander
Post Reply