March 02, 2021, 01:19:27 PM

Author Topic: [bb] Bit Fiddling (Blitz3D / B+ version) by _PJ_ [ 1+ years ago ]  (Read 479 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Bit Fiddling (Blitz3D / B+ version)
Author : _PJ_
Posted : 1+ years ago

Description : A BB version of the Bit Fiddling code by Shortwind

Code :
Code: BlitzBasic
  1. ; Thansk Shortwind for the original. These are useful functions and so I thought it handy to provide the BB version just in case.
  2.  
  3. ;x=the Int with the value, bit is the bit to set, ie. 0-31
  4. ; Note: bits start at Bit 0 = 1, Bit 1 =2, Bit 3=4 etc...
  5.  
  6. Function SetBit%(x%,Bit%)
  7.         Return x% Or (1 Shl Bit)
  8. End Function
  9.  
  10. Function ClearBit%(x%,Bit%)
  11.         Return x% And(x Xor (1 Shl Bit))
  12. End Function
  13.  
  14. Function ToggleBit%(x%,Bit%)
  15.         Return x%Xor(1 Shl Bit%)
  16. End Function
  17.  
  18. Function ReadBit(x%,Bit%)
  19.         Return (x% And (1 Shl Bit%))
  20. End Function


Comments :


jute(Posted 1+ years ago)

 I didn't know you could do this!.....interesting...


RifRaf(Posted 1+ years ago)

 thanks


Charrua(Posted 1+ years ago)

 these are the one's i usually use:
Code: [Select]

Function FlagOnOff(Flags, FlagNumber, State)
Mask = 1 Shl FlagNumber
If State=1 Then
Return Flags Or Mask
Else
Return Flags And ( $FFFFFFFF Xor Mask )
End If
End Function

Function TestOnOff(Flags, FlagNumber)
Return (Flags And (1 Shl FlagNumber)) <> 0
End Function
basically the sameJuan


Dan(Posted 1+ years ago)

 Ok earlier today i was thinking about my current project, then i got a idea to use bits to save few menu states in it.So i wrote few functions: BitSet, BitUnSet and BitCheck.Here is the demo app which i wrote to test the functions:(included is aswell TextY function which does (almost) the same thing as Print but in Graphic mode.)
Code: [Select]
;====================================================================
; Project: BitSetUnset function demo
; Version: 1.00
; Author: Dan
; Email:
; Copyright: PD
; Description:
;   Test for the bit set functions  
;   Instead of searching the code archives on www.blitzbasic.com i wrote
;   few routines to set unset and check bits. then i wrote this test
;   app to check the functionality. after it was done i found out there
;   was allready few functions from _PJ_ in the archive
;   so i expanded this test utility to test all functions.
; Usage:
; Left + right mouse button to  increase the tested variable
; Hold left Shift button to slowdown
; Esc Switches to the next test
; The Graphic window is resizable
;====================================================================

HY=950              ;change the vertical resolution to show more/less numbers
Graphics 555,hy,32,2
Graphics 555,hy,32,3  

;Font=LoadFont("Amiga Forever",20,True,False,False)
;Font=LoadFont("MyAmigaFont",15,False,False,False)
;Font=LoadFont("Blitz",18,False,False,False)
Font=LoadFont("Courier New",18,True,False,False)

SetFont Font
Global d=5                  ;Delay between mouseclicks
Global yy ;yy = Variable to play with
Global TxtY=-2 ; Used for my TextY function
Global Timer=MilliSecs() ;Flip delay timer

FramesPerWaitAMinute=10 ;Flip delay variable

Range=HY/(950/(24-1)) ; Calculate how many bit calculation could be displayed with "Courier new" font
;Range 16                   ; Can be set manually as well, uncomment this line


AppTitle "Test #1: Set bit #"
Repeat

MouseIt()

Gosub Title

For x=0 To Range-1
TextY (0,LSet(x+"   ",5)+" = "+Bin$(SetBit(yy,x))+" = "+SetBit(yy,x))
Next
; TextY (1,"")
TextY (1,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))
For x=1 To Range
TextY (0,LSet(x+" =",5)+" = "+Bin$(BitSet(yy,x))+" = "+BitSet(yy,x))
Next
Flips(FramesPerWaitAMinute)
Until KeyDown(1)

AppTitle "Test #2: Clear bit #"

Delay 250
FlushKeys()
FlushMouse()

Repeat
MouseIt()
Gosub Title

For x=0 To Range-1
TextY (0,LSet(x+"   ",5)+" = "+Bin$(ClearBit(yy,x))+" = "+LSet(ClearBit(yy,x),7))
Next
; TextY (1,"")
TextY (1,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))
For x=1 To Range
TextY (0,LSet(x+"   ",5)+" = "+Bin$(BitUnSet(yy,x))+" = "+LSet(BitUnSet(yy,x),7))
Next
Flips(FramesPerWaitAMinute)
Until KeyDown(1)

Delay 250
FlushKeys()
FlushMouse()

AppTitle "Test #3: read bit #"

Repeat
MouseIt()
Gosub Title

For x=0 To Range-1
TextY (0,LSet(x+"   ",5)+" = "+Bin$(yy)+" = "+ReadBit(yy,x))
Next
; TextY (1,"")
TextY (1,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))
For x=1 To Range
TextY (0,LSet(x+"   ",5)+" = "+Bin$(yy)+" = "+BitState(yy,x))
Next
    Flips(FramesPerWaitAMinute)
Until KeyDown(1)

End

.Title
Cls
TxtY=-2
TextY (-2,LSet(yy,5)+" = "+Bin$(yy))
TextY (0,"")
TextY (0,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))

Return

Function Flips(a)
If Timer-MilliSecs()>a
  Flip
  Timer=MilliSecs()
End If
End Function

Function MouseIt()
    If KeyDown(42)
 dd=100
Else
 dd=d
EndIf
If MouseDown(1)
yy=yy+1
Delay dd
ElseIf MouseDown(2)
yy=yy-1
Delay dd
EndIf
End Function


;---------------------Bit functions by Dan----------------------------------------------------
Function BitSet(a,b)
;a = variable
;b = bit number to set (1 is the 1st bit)

y=1
For x=0 To b
If x<=1
y=x
Else
y=y*2
EndIf
Next
Return y Or a
End Function

Function BitUnSet(a,b)
;a = variable
;b = bit number to unset (1 is the 1st bit)
y=1
For x=0 To b
If x<=1
y=x
Else
y=y*2
EndIf
Next
Return  (y And a) Xor a
End Function

Function BitState(a,b)
;a = variable
;b = bit number to Check (1 is the 1st bit)
a=Mid(Bin$(a),Len(Bin$(a))-(b-1),1)
Select a
Case "0"
Return False
Case "1"
Return True
End Select
End Function

;-------------------------------------------------------------------------


;======================Bit Functions by PJ===================================================

Function SetBit%(x%,Bit%)
  Return x% Or (1 Shl Bit)
End Function

Function ClearBit%(x%,Bit%)
  Return x% And(x Xor (1 Shl Bit))
End Function

Function ToggleBit%(x%,Bit%)
Return x%Xor(1 Shl Bit%)
End Function

Function ReadBit(x%,Bit%)
Return (x% And (1 Shl Bit%))
End Function

;=========================================================================


Function TextY (num,txt$)
;Num = How many columns to skip
;Replaces Print with Text
;Every Function call displays the text
;1 line under the old one, like calling
;multiple print statements after eachother.
;
; Set num to -2 to make the text go on top (like locate 0,0)
; Use the Global TxtY=-2 outside this function
;
;Global TxtY=-2       ;Make TxtY global variable for displaying help text
   If num=-2
      TxtY=0
   Else
 TxtY=TxtY+(FontHeight()*(num+1))
   EndIf
   Text 0,TxtY,txt$
End Function
The bit numbers (1...16...) to the left are Either Set , Unset, or checked. The displayed binary to their right side shows the result, and Decimal result of it is shown aswell.~Dan~


Dan(Posted 1+ years ago)

 ok iv updated this App to use ToggleBit command, now its called Bit Workshop and has a buttons for each test, press esc to go back to the menue.
Code: [Select]
;====================================================================
; Project: BitSetUnset function demo
; Version: 1.01
; Author: Dan
; Email:
; Copyright: PD
; Description:
;   Test for the bit set functions  
;   Instead of searching the code archives on www.blitzbasic.com i wrote
;   few routines to set unset and check bits. then i wrote this test
;   app to check the functionality. after it was done i found out there
;   was allready few functions from _PJ_ in the archive
;   so i expanded this test utility to test all functions.
; Usage:
; Left + right mouse button to  increase the tested variable
; Hold left Shift button to slowdown
; Esc Switches to the next test
; The Graphic window is resizable
;====================================================================

HY=950              ;change the vertical resolution to show more/less numbers
Graphics 555,hy,32,2
Graphics 555,hy,32,3  

;Font=LoadFont("Amiga Forever",20,True,False,False)
;Font=LoadFont("MyAmigaFont",15,False,False,False)
;Font=LoadFont("Blitz",18,False,False,False)
Font=LoadFont("Courier New",18,True,False,False)

SetFont Font
Global d=5                  ;Delay between mouseclicks
Global yy ;yy = Variable to play with
Global TxtY=-2 ; Used for my TextY function
Global Timer=MilliSecs() ;Flip delay timer
Global MD
Quit=0

Range=HY/(950/(24-1)) ; Calculate how many bit calculation could be displayed with "Courier new" font
;Range 16                   ; Can be set manually as well, uncomment this line

SetBuffer BackBuffer()


;MainLoop

Repeat
Cls
Color 255,255,255
TextY (-2,"**************************************************")
TextY (0 ,"**************    BIT Workshop    ****************")
TextY (0 ,"**************************************************")


Select True
Case DrawButton(0,10,60,100,25,"Bit Set")
 If MD=1 Then Gosub Test1
Case DrawButton(0,10,90,100,25,"BitUnset")
 If MD=1 Then Gosub Test2
Case DrawButton(0,10,120,100,25,"Bit Check")
 If MD=1 Then Gosub test3
Case DrawButton(0,10,150,100,25,"Bit Flip")
 If MD=1 Then Gosub Test4
Case DrawButton(0,10,200,100,25,"Quit")
Quit=1
End Select
TextY (10,"MX="+MouseX()+"  MY="+MouseY())

Delay 10
Flip
Until QUIT=1

End

.Test1
MZ_clicked=0

FlushIt("Test #1: Set bit #")

Repeat

MouseIt()

Gosub Title

For x=0 To Range-1
TextY (0,LSet(x+" +1",5)+" = "+Bin$(SetBit(yy,x))+" = "+SetBit(yy,x))
Next
; TextY (1,"")
    Color 241,248,3
TextY (1,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))
Color 255,255,255
For x=1 To Range
TextY (0,LSet(x+" =",5)+" = "+Bin$(BitSet(yy,x))+" = "+BitSet(yy,x))
Next
Flip
Until KeyDown(1)
FlushIt ("Main Menue")

Return

.Test2
MZ_clicked=0
FlushIt ("Test #2: Clear bit #")

Repeat
MouseIt()
Gosub Title

For x=0 To Range-1
TextY (0,LSet(x+" +1",5)+" = "+Bin$(ClearBit(yy,x))+" = "+LSet(ClearBit(yy,x),7))
Next
; TextY (1,"")
    Color 241,248,3
TextY (1,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))
Color 255,255,255
For x=1 To Range
TextY (0,LSet(x+"   ",5)+" = "+Bin$(BitUnSet(yy,x))+" = "+LSet(BitUnSet(yy,x),7))
Next
Flip
Until KeyDown(1)
FlushIt ("Main Menue")
Return

.test3
Z_clicked=0

FlushIt("Test #3: read bit #")

Repeat
MouseIt()
Gosub Title

For x=0 To Range-1
TextY (0,LSet(x+" +1",5)+" = "+Bin$(yy)+" = "+ReadBit(yy,x))
Next
; TextY (1,"")
    Color 241,248,3
TextY (1,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))
Color 255,255,255
For x=1 To Range
TextY (0,LSet(x+"   ",5)+" = "+Bin$(yy)+" = "+BitState(yy,x))
Next
    Flip
Until KeyDown(1)
AppTitle "Main Menue"
Return

.Test4
Z_clicked=0
FlushIt ("Test #4: Bit Flip #")

Repeat
Cls
Color 255,255,255
TextY (-2,"Variable Value :"+yy)
TextY (1,"Set bits by clicking the Squares")
TextY (4,"   "+Bin$(yy))
y=0
For x=1 To 32  
;      Cls
;  Delay 100
;  Text 1,1,"Y="+Y
      y=y+17
 If DrawButton(BitState(yy,x),550-y,60,16,43,"x")
  If MD>0
    yy=ToggleBit(yy,x-1)
Repeat
Delay 1
Until MouseDown(1)=0 And MouseDown(2)=0
  EndIf
 EndIf
Next
 


Flip
Until KeyDown(1)

FlushIt ("Main Menue")
Return

.Title
Cls
TxtY=-2
TextY (-2,LSet(yy,5)+" = "+Bin$(yy))
TextY (0,"")
    Color 241,248,3
TextY (0,LSet("bit",8)+LSet("Binary",33)+LSet("= Result",10))
    Color 255,255,255
Return

Function FlushIt(txt$)
AppTitle txt$
Delay 100
FlushKeys()
FlushMouse()
Color 255,255,255
End Function

Function MouseIt()
    If KeyDown(42)
 dd=100
Else
 dd=d
EndIf
If MouseDown(1)
yy=yy+1
Delay dd
ElseIf MouseDown(2)
yy=yy-1
Delay dd
EndIf
End Function


;---------------------Bit functions by Dan----------------------------------------------------
Function BitSet(a,b)
;a = variable
;b = bit number to set (1 is the 1st bit)

y=1
For x=0 To b
If x<=1
y=x
Else
y=y*2
EndIf
Next
Return y Or a
End Function

Function BitUnSet(a,b)
;a = variable
;b = bit number to unset (1 is the 1st bit)
y=1
For x=0 To b
If x<=1
y=x
Else
y=y*2
EndIf
Next
Return  (y And a) Xor a
End Function

Function BitState(a,b)
;a = variable
;b = bit number to Check (1 is the 1st bit)
a=Mid(Bin$(a),Len(Bin$(a))-(b-1),1)
Select a
Case "0"
Return False
Case "1"
Return True
End Select
End Function

;-------------------------------------------------------------------------


;======================Bit Functions by PJ===================================================

Function SetBit%(x%,Bit%)
  Return x% Or (1 Shl Bit)
End Function

Function ClearBit%(x%,Bit%)
  Return x% And(x Xor (1 Shl Bit))
End Function

Function ToggleBit%(x%,Bit%)
Return x%Xor(1 Shl Bit%)
End Function

Function ReadBit(x%,Bit%)
Return (x% And (1 Shl Bit%))
End Function

;=========================================================================


Function TextY (num,txt$)
;Num = How many columns to skip
;Replaces Print with Text
;Every Function call displays the text
;1 line under the old one, like calling
;multiple print statements after eachother.
;
; Set num to -2 to make the text go on top (like locate 0,0)
; Use the Global TxtY=-2 outside this function
;
;Global TxtY=-2       ;Make TxtY global variable for displaying help text
   If num=-2
      TxtY=0
   Else
 TxtY=TxtY+(FontHeight()*(num+1))
   EndIf
   Text 0,TxtY,txt$
End Function

Function DrawButton(filled,x,y,xw,yw,txt$)
MB=0
Select filled
Case 1
Color 131,142,245
Rect x+1,y+1,xw-1,yw-1,1
Color 17,5,237
Rect x,y,xw,yw,0
Color 255,255,255
Text x+3,y+3,txt$
Case 0
Color 131,142,245
Rect x,y,xw,yw,0
Color 17,5,237
Rect x,y,xw,yw,0
Color 47,198,54

Text x+3,y+3,txt$
End Select

Return CheckZoneClick(x,y,x+xw,y+yw)

Color 255,255,255
End Function


Function CheckZoneClick(x,y,x1,y1)
 
  MC=0
  MD=0
  If MouseDown(1) Then MD=1
  If MouseDown(2) Then MD=2
  If MD>0
    If (MouseX()=>x And MouseX()<=x1) And (MouseY()=>y And MouseY()<=y1) Then MC=True
  EndIf
Return MC
End Function
~Dan~


BlitzSupport(Posted 1+ years ago)

 That's pretty cool, though would be better with the ability to step through values one at a time rather than fly through with the mouse. I'll probably do this myself, as it does make visualising what's going on really easy. Nice one!


Dan(Posted 1+ years ago)

 hmm if you hold left shift down, then you can slowdown it a bit.if it is still too fast, modify the
Code: [Select]
Function MouseIt()
    If KeyDown(42)
 dd=100
Else
 dd=d
EndIf
change dd=100 variable to 200 or higher (=delay in milisecs) [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal