Bitwise operators and conditions
BlitzMax Forums/BlitzMax Programming/Bitwise operators and conditions
| ||
Hi, I'm trying to achieve something like this (pseudocode), but this doesn't seem to work: If (keyhit(LEFT) and (state & not STATE_LEFT)) OR (keyhit(RIGHT) and (state & STATE_LEFT)) and not "turning" then state :+ STATE_TURNING do "turning"... etc end if It's about the (state & not STATE_LEFT), I don't think it works that way. Can anyone give me his/her input on how to fix this? SQD |
| ||
you need to explain exactly what each part is supposed to do. There are several ways of doing it. the 'and' compares two values and returns true or false (0 or 1).the 'Not' will allways return true or false (0 or 1). The '&' is a bit operator it works on bits. If the value 3(%0011) is '&' with 8(%1000) the result is 0(000). if 3(%0011) is '&' with 9(%1001) the result is 1. if 10(%1010) is '&' with 8(%1000) the result is 8. first example: %0011 %1000 & ---- %0000 second example: %0011 %1001 & ---- %0001 third example: %1010 %1000 & ------ %1000 (8) so unless you know what you are trying to acomplish on each comparison it is really difficult to help you. |
| ||
Not entirely sure what you're trying to do but...Not(state & STATE_LEFT)I think you need to explain a bit more. |
| ||
You probably want bitwise not instead of boolean not.state & ~STATE_LEFT |
| ||
grable wrote: You probably want bitwise not instead of boolean not. I'm not so sure he does. I think he's doing a bit flag check. In which case that code will return true if any of the other bit flags are set regardless of the state of the STATE_LEFT bit flag, but of course, that may be exactly what he's trying to do. This is why we need more explination before we can help. state & ~STATE_LEFT |
| ||
Careful there, I don't think State & ~STATE_LEFT is the same as ~(State & STATE_LEFT). |