Bitwise operations??
BlitzMax Forums/BlitzMax Beginners Area/Bitwise operations??
| ||
| Hello, I'm trying to fingure out how to do bitwise operations in Max. All I want to do is be able to set or clear bits based on a bitmask.. For example, in other languages, & and || always did this fine. For some reason it isn't porting over to max well for me. Here is a simple test i whipped up. I have a value of 3 being held in a variable called flag (bits 1 and 2 set). I am trying to use & and | to toggle bit 1 on/off... Which should result in the flag variable toggling between 2 and 3.. DOesn't seem to be working... I understand I can use XOR for this, but i don't simply want to toggle the bits, I want to set them if not set, and/or clear them if not cleared.. Here is my sample code. Am I doing something wrong, or is this Max related??
Graphics 640,480
Global Flag:Byte = 3
Global bitmask:Byte = 1
While Not KeyDown(KEY_ESCAPE)
Cls
DrawText("BITWISE OPERATOR TEST",10,10)
DrawText("Use 7 to do an & operation (bitmask of "+bitmask+")",10,30)
DrawText("Use \ to do an | operation (bitmask of "+bitmask+")",10,50)
DrawText("Flag Value: " + flag,10,70)
If KeyHit(KEY_7) '& operation
flag = (flag & bitmask)
EndIf
If KeyHit (KEY_BACKSLASH) '| operator
flag = (flag | bitmask)
EndIf
FlushMem
Flip
Wend
End
|
| ||
| I think you do something wrong flag & bitmask -> bitmask and after that flag | bitmask -> bitmask as flag is already bitmask. I think this is what you wanted:
Graphics 640,480,0
Global Flag:Byte = 3
Global bitmask:Byte = 1
While Not KeyDown(KEY_ESCAPE)
Cls
DrawText("BITWISE OPERATOR TEST",10,10)
DrawText("Use 7 to do an & operation (bitmask of "+bitmask+")",10,30)
DrawText("Use \ to do an | operation (bitmask of "+bitmask+")",10,50)
DrawText("Flag Value: " + flag,10,70)
If KeyHit(KEY_7) '& operation
flag = (flag & ~bitmask)
EndIf
If KeyHit (KEY_space) '| operator
flag = (flag | bitmask)
EndIf
FlushMem
Flip
Wend
End
|
| ||
| aah, i see... Thank you very much for your help! |