FlipBool with bitwise operators

edited March 2008 in Vanilla 1.0 Help
function FlipBool($Bool) { $Bool = ForceBool($Bool, 0); return $Bool?0:1; }
becomes

function FlipBool($Bool) { $Bool = ForceBool($Bool, 0); return $Bool ^ 1; }
The performance gains will be huge! I say this patch be added to the core post-haste.

Comments

  • How does this work?
  • edited March 2008
    It was just a random musing, I'm not being totally serious here. It just replaces the conditional statement with a bitwise operator. Both achieve the same thing really. The bitwise operator '^' is the XOR operator. Whereas a normal OR operator returns true if either inputs are 1, the XOR operator only returns true if one of the inputs are 1. Thus if $Bool is equal to 0 then, when compared to '1' with a XOR operator, the result will be 1 because one of the inputs is one (the second input) but not both of them. If $Bool is equal to 1 then both inputs will be 1 and so the XOR operation will evaluate to false, or 0. All that has the effect of flipping the bits. A 1 becomes a 0 and vice versa.
  • We could also do ~$Bool or (Not $bool) It does make me wonder which is better performance-wise. Not like it really matters much, searching the codebase tells me it runs at most once per comment on a page load, among other places.
  • Ahh I see. I didnt realise ^ was an operator, I thought it was a mathematical 'power' function - i.e. raise $Bool to the power 1 - but that wouldn't change it. We could use ($Bool -1)^2.
  • edited March 2008
    Yeah I was kidding about the performance gains really. I can't see it making that much difference. Funnily enough, my first thought was to use the ~ operator too but I did some testing and it had really weird results. ~0 evaluates to -1 while ~1 evaluates to -2. Probably something to do with how PHP handles integers internally. @Minisweeper: Heh, yeah in PHP you have to use the pow() function which seems a bit round-about to me. We could also use your example with the XOR operator... or any number of other examples - gotta love those logic operations.
  • I was wondering about that... It seems 0 is stored as 0000 (32 bits though) and -1 is stored as 1111, so performing ~0 flips all the bits on. I had always assumed -1 was stored like 1001, one bit for the negative sign and counting up, just like a regular integer... but negatives count down. That also explains ~1, it turns 0001 into 1110. I'm plugging all these numbers into Window's calculator--it grays out all number buttons but 1 and 0 when turn on binary mode!
  • NickENickE New
    edited March 2008
    http://en.wikipedia.org/wiki/Two's_complement

    it's actually quite logical, in part because negative numbers are the same distance from 0 as their positive counter-parts. converting between positive and negative values is interesting: (a is 8 bits)-a = 0 - a = 1111 1111 + 1 - a = (1111 1111 - a) + 1 = ~a + 1
    and allows computers to easily subtract numbers: a - b can be seen as a + ~b + 1

    and logical not (!) would probably be a better operator to use the the boolean not.
This discussion has been closed.