What's the opposite of "|"?

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

JoshK(Posted 2008) [#1]
I forgot how you remove bits from flag values.

state = thing | thing3

What is the opposite, to remove a state?


GW(Posted 2008) [#2]
this?

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


SebHoll(Posted 2008) [#3]
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.


markcw(Posted 2008) [#4]
Xor is the opposite of Or for bitwise operators, don't know the symbol in Bmax though.


markcw(Posted 2008) [#5]
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.


plash(Posted 2008) [#6]
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)



Koriolis(Posted 2008) [#7]
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 "& ~")


Grey Alien(Posted 2008) [#8]
hah, I had to find out the exact same thing last week. Used method markcw said.


Yan(Posted 2008) [#9]
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]


Grey Alien(Posted 2008) [#10]
nice thanks


ImaginaryHuman(Posted 2008) [#11]
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.


Yan(Posted 2008) [#12]
...See edit above...


Grey Alien(Posted 2008) [#13]
ImaginaryHuman: In fact that's what I did exactly in a recent bit of code.