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