Jump to content

Boolean


pennineacute

Recommended Posts

I am after some help for a project I am doing pls.

I am writing a program to display the truth table of a boolean expression and so far all is good.

I decided to convert the input into reverse polish notation, so I could get the expression into priority order.  This works.

The program displays the truth table inputs, with no problems, and I am now working on the Boolean part.

If you entered for eg, a+b.c, I have stored the reverse polish notation in a array.  In this would be stored a b c . + in elements 0 to 4

As each row input is calculated eg

a b c    output

0 0 0      

I then want to display the output.

My first idea was to read through the array until I hit a boolean operator and then perform this with the top two variables in my stack, in this case a.b

but when trying values, I can get a boolean output for and, or and xor, but not for NOT, as can be see from the attachment

My program is live at http://andylittlewood.co.uk

The coding I was using to test the boolean output is
 

				echo "1 and 1 = "; echo 1 && 1;
				echo "<br>1 or 1 = "; echo 1 || 1;
				echo "<br> 1 xor 1 = "; echo true ^ true;
				echo "<br>Not 1 = ";echo !(true);

I cannot put my full coding on here, as it is a project and do not want to plagiarise. 

 

post-187557-0-47888200-1447271590_thumb.png

Link to comment
Share on other sites

 

echo true ^ true;
echo !(true);

 

You're suffering from PHP's loose typing. ^ is not a logical operator but a bitwise operator. It acts on numbers. true^true is interpreted as 1^1. The answer is, of course, 0.

However ! is a logical operator. !true is false, but PHP decided that the string representation (ie, what you get when you try to echo it) is empty.

 

Try

echo "1 and 1 = "; echo (1 && 1 ? "1" : "0");
echo "<br>1 or 1 = "; echo (1 || 1 ? "1" : "0");
echo "<br> 1 xor 1 = "; echo (1 ^ 1 ? "1" : "0");
echo "<br>Not 1 = "; echo (!1 ? "1" : "0");
or

function showvalue($expr) {
	echo $expr ? "1" : "0";
}

echo "1 and 1 = "; showvalue(1 && 1);
echo "<br>1 or 1 = "; showvalue(1 || 1);
echo "<br> 1 xor 1 = "; showvalue(1 ^ 1);
echo "<br>Not 1 = "; showvalue(!1);
You should also (re)acquaint yourself with PHP's various operators. I suggest you stick with the bitwise operators only, meaning use & and | instead of && and ||.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.