Code archives/Algorithms/Bit Fiddling (Blitz3D / B+ version)

This code has been declared by its author to be Public Domain code.

Download source code

Bit Fiddling (Blitz3D / B+ version) by _PJ_2010
A BB version of the Bit Fiddling code by Shortwind
; Thansk Shortwind for the original. These are useful functions and so I thought it handy to provide the BB version just in case.

;x=the Int with the value, bit is the bit to set, ie. 0-31
; Note: bits start at Bit 0 = 1, Bit 1 =2, Bit 3=4 etc...

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

Comments

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


RifRaf2010
thanks


Charrua2010
these are the one's i usually use:


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 same

Juan


Dan2015
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.)



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~


Dan2015
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.



~Dan~


BlitzSupport2015
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!


Dan2015
hmm if you hold left shift down, then you can slowdown it a bit.
if it is still too fast, modify the
Function MouseIt()
    If KeyDown(42) 
	  dd=100
	Else 
	  dd=d
	EndIf


change dd=100 variable to 200 or higher (=delay in milisecs)


Code Archives Forum