binary arithmatic

BlitzMax Forums/BlitzMax Beginners Area/binary arithmatic

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here are functions for this shift in variable bitlength including a short sample:

b=%1010
For i=1 To 33
	Print Bin(b)
	b=whr(b,4)
Next
b=%1010
For i=1 To 33
	Print Bin(b)
	b=whl(b,4)
Next


Function WHR:Int(number:Int, bitlength:Int=32)
	If number & 1
		Return ( (number Shr 1) | (%1 Shl (bitlength-1)))
	Else
		Return (number Shr 1)
	EndIf
End Function

Function WHL:Int(number:Int, bitlength:Int=32)
	If number & (1 Shl (bitlength-1))
		Return ( ((number Shl 1) ~ (1 Shl (bitlength)) )| 1 )
	Else
		Return (number Shl 1)
	EndIf
End Function