What's the opposite of "|"?

BlitzMax Forums/BlitzMax Programming/What's the opposite of "|"?

I forgot how you remove bits from flag values.

state = thing | thing3

What is the opposite, to remove a state?

this?

Print Bin((64+128))
Print Bin((64+128)~128)

What is the opposite, to remove a state?

You can remove a bitwise flag using "&~", e.g. "state:&~thing3" (literally, AND NOT thing3). I find it easy to remember that way.

Xor is the opposite of Or for bitwise operators, don't know the symbol in Bmax though.

One way to remove a single bit, & the value with the bit you want, if it's true then subtract the bit's value from the original value.

http://www.blitzbasic.com/Community/posts.php?topic=77276#864589

Use the '~' operation, 'Flags:~ FLAG_THREE'.

(You can also use integers for flags and flag holders)
SuperStrict

Framework BRL.StandardIO

Const FLAG_ONE:Byte = 1, FLAG_TWO:Byte = 2, FLAG_THREE:Byte = 4, FLAG_FOUR:Byte = 8, FLAG_FIVE:Byte = 16

Local Flags:Byte = 0

'Add a flag to a variable
Flags:| FLAG_THREE

'Check if a variable contains a flag
If Flags & FLAG_THREE Then Print "Hello, flag three!"

'Remove a flag from a variable
Flags:~ FLAG_THREE

'Check if a variable contains a flag (again...)
If Flags & FLAG_THREE
	Print "Hello, flag three!"
	
Else
	Print "Flag three has left! :("
	
End If

Flags:| FLAG_ONE | FLAG_TWO | FLAG_THREE

'Invert flags
Local Flags_Inverted:Byte = ~Flags

If Flags_Inverted & FLAG_ONE Then Print "ONE is here!"
If Flags_Inverted & FLAG_TWO Then Print "TWO is here!"
If Flags_Inverted & FLAG_THREE Then Print "THREE is here!"
	
If Flags_Inverted & FLAG_FOUR Then Print "FOUR is here!"
If Flags_Inverted & FLAG_FIVE Then Print "Five is here!"


BITWISE XOR will TOGGLE a bit, not erase it (in other words, if it's not already set, it will set it, otherwise it will erase it).
As Sebholl said, you need to use BITWISE AND NOT (in BlitzMax "& ~")

hah, I had to find out the exact same thing last week. Used method markcw said.

I know this thread is pretty much dead and buried but, as this kind of question seems to get asked quite often, I'm adding this for future reference...
Function setBit(flag% Var, bit%)
    flag :| bit
End Function

Function clearBit(flag% Var, bit%)
    flag :& ~bit
End Function

Function toggleBit(flag% Var, bit%)  
    flag :~ bit
End Function

Function testBit%(flag%, bit%)
    If (flag & bit) Then Return True
End Function


...and...

SuperStrict

Local flag%

SetBit(flag, 7)
Print Bin(flag)
Print CheckBit(flag, 7)
Print 
ClearBit(flag, 7)
Print Bin(flag)
Print CheckBit(flag, 7)
Print 
ToggleBit(flag, 7)
Print Bin(flag)
Print CheckBit(flag, 7)

End


Function CheckBit%(value%, bit%)
  Return (value & (1 Shl bit)) Shr bit
End Function

Function SetBit%(value% Var, bit%)
  value :| (1 Shl bit)
End Function

Function ClearBit%(value% Var, bit%)
  value :& ~(1 Shl bit)
End Function

Function ToggleBit%(value% Var, bit%)  
  value :~ (1 Shl bit)
End Function


[edit]
For the benefit of those who are yet to master the subtle intricacies of hyperlinks, I've copied the code from the, previously linked, thread. ;o)
[edit]

nice thanks

You can also ask it to set or test a given bit index number, rather than pass a bit pattern, by using:

Mask=$1 Shl BitIndex

to generate a bitmask.

...See edit above...

ImaginaryHuman: In fact that's what I did exactly in a recent bit of code.