Bitwise

Hi guys,

I’ve been trying to work out how best to express this in PHP:

b OR (Value2 AND/OR Value3) must be set[/b]

Basically, if this is NOT the case, then I need to throw an exception. So, I started off with something like:

if (
 (isset($val1) && !empty($val1)) ||
 (isset($val2) && !empty($val2)) ||
 (isset($val3) && !empty($val3))
){
  echo "We have a match";
}

That would ensure that at least one of the values is set but I need to know when only $val1 is set or when any combination of $val2 and $val3 are set.

I thought this would be a good way to try bitwise but I couldn’t get the answer that I wanted. I’ve ended up with a pseudo bitwise I guess, as follows:

$bw1    = (isset($val1) && !empty($val1)) ? 1 : 0;
$bw2    = (isset($val2) && !empty($val2)) ? 2 : 0;
$bw3    = (isset($val3) && !empty($val3)) ? 4 : 0;
$bwtot  = $bw1 + $bw2 + $bw3;
$accept = array(1, 2, 4, 6);

if (!in_array($bwtot, $accept)){
    echo "<p>Failed: $bwtot</p>";
}

Not exactly the way I wanted to do it, but it does work. I know there’s a better way and bitwise may be the way to go, so I’m interested in a) alternative solutions and b) the correct way to do this with bitwise.

Thanks for your help guys

Um, no offence intended, but how is that better than this?

$bw1    = (isset($val1) && !empty($val1)) ? 1 : '';
$bw2    = (isset($val2) && !empty($val2)) ? 2 : '';
$bw3    = (isset($val3) && !empty($val3)) ? 4 : '';

if (!empty($bw1) && (!empty($bw2) || !empty($bw3))){
    echo "There was an error";
}

Hope that this example will make more clear and where to throw exception, you will manage to do it yourself.

<?php
$value1=‘value1’;
//OR
$value2=‘value2’;
//AND/OR
$value3=‘’;
//must be set

if(isset($value1) && $value1!=‘’){
print ‘value ONE is set’;
} else if(isset($value2) && $value2!=‘’ && isset($value3) && $value3!=‘’){
print ‘value 2 AND value 3 is set’;
} else if(isset($value2) && $value2!=‘’ || isset($value3) && $value3!=‘’){
print ‘value 2 OR value 3 is set’;
}
?>

But it’s irrelevant in that example, since it’s not using bitwise, it’s using TRUE/FALSE validation

lol, I deleted that as soon as I saw another reply … me thinks it’s the caching, not the wine.

I still get your replies by email… there is no delete option for you :stuck_out_tongue:

Now I feel daft. This gives the perfect solution since I need to know when the conditions are NOT met, rather than when they are:

if (!empty($val1) && (!empty($val2) || !empty($val3))){
    echo "There was an error";
}

Just re-read your previous post, I had misread it first time around. You’re suggesting that I set it up so that the values added to $sum will result in my matches failing if $sum > 4? Yes, that could work, but it’s still not truly bitwise, is it?

heh, SP let me blissfully edit my post while not showing your replies :slight_smile:

basically, I don’t see your attraction to bitwise really, but based on the odd/even nature you posted, then & 1 (4&1 is 0, 3&1 is 1) will give you what you want if you convert the isset()'s to an integer using binary arithmetic.

it would seem that a smiple % 2 would be your answer, bitwise, if the least significant bit == 1 then it fails, so ! & 001

Not sure if I was confused before or it’s the wine I had with dinner, but my first interpretation was based on var1 being set, or either of the others -> x < 4 and > 0

> 4 could be 5, 6 or 7. The only acceptable one of these is 6
= 4 is acceptable
< 4 could be 1, 2 or 3. 1 and 2 are acceptable, 3 is not

So we can’t say > or < anything is acceptable. However, using bitwise operators ^ (XOR), | (OR) and & (AND) rather than the usual PHP || and &&, I would say something like:

$sum MUST be equal to $val1 ^ ($val2 | $val3)

Meaning, if $sum == $val1 then neither $val2 or $val3 must be set, because it’s an exclusive-or (XOR). If however $sum != $val1, then $sum must be equal to $val2 or $val3 or both.

That’s how I understand it, though I may have written it incorrectly. It’s just how to actually implement this in PHP that I’m stuck with

Also not sure after that, is this the pattern?


$sum = 0;
if(isset($v1)) $sum += 4
if(isset($v2)) $sum += 2
if(isset($v3)) $sum += 1

// > 4 means 1 and (2 or 3) isset
// = 4 means 1 only
// < 4 means 2 or 3 only

Edit, after I realised that “Not sure I quite follow you” was referring to you, it would seem that a smiple % 2 would be your answer, bitwise, if the least significant bit == 1 then it fails, so ! & 001

Nobody at all? :frowning:

Which would translate to >5, do you want that bitwise? or logical? :stuck_out_tongue:

[QUOTE=hash;]Actually category c
I had a little fun working with bits (been a while since I’ve done that), and you learned how to phrase questions, and hopefully what bitwise means :)[/QUOTE]Lost me on that one. I thought the question was phrased perfectly OK, and I know what bitwise is. You should lay off the wine at dinner time :lol:

Not sure I quite follow you, but technically, the following are acceptable:

001 == 1
010 == 2
100 == 4
110 == 6

Unacceptable values are:

000 == 0
011 == 3
101 == 5
111 == 7

This is why I just used the array and !in_array() to determine whether the value was acceptable or not

OK, I’m off with that one, you specifically asked for an alternate solution to one you already had that worked, and specified bitwise. Obviously I keep getting it wrong, so nn :slight_smile:

Ha ha, no, I meant that I was looking for both alternatives in how to smoothly perform the same validation without bitwise, but also how to do it WITH bitwise. So I’ve now got a simple, logical method to validate the variables, which meets one of my criteria, and I’m happy with that, but I would still like to better understand whether I can use bitwise or not in order to get the same result

So this one falls in to category a) :smiley:

and b) the correct way to do this with bitwise

but I’d rather now know how to get the result I’m after using bitwise

or not.

PHP actually has the XOR operator.
So you can just use


if (!empty($val1) xor (!empty($val2) or !empty($val3)) {
  echo "We have a match";
}