Simple boolean question?

Simple boolean question?

• 13 Maret 2012 21:29

Hello,

I would like to confirm something. For sample purpose let IDX be 250 and if we have:

((IDX == 250) XOR  IDX > 270)

we can easily simplify from left to right like this:

> (TRUE XOR FALSE)

> (TRUE)

However, if I throw in an expression with its own brackets in the middle of the above expression I am not sure if we should be solving from right to left or left to right?

Consider the following:

((IDX == 250) XOR (IDX !=1) AND IDX > 270)

it now simplifies to:

((TRUE) XOR (TRUE) AND FALSE)

So what should be the solving order... I am doing it from right to left like this:

>   ((TRUE) XOR FALSE)

>  (TRUE)

However if we do it from left to right we get a different answer:

consider the following:

((TRUE) XOR (TRUE) AND FALSE)

>   (FALSE AND FALSE)

>  (FALSE)

Just to make sure here we should be solving from right to left.... right?

thanks for all feedback

sorry for the lame question...

r

• Diedit oleh 13 Maret 2012 21:31
•

Semua Balasan

• 13 Maret 2012 21:44

On 3/13/2012 5:29 PM, roberto wrote:

Consider the following:

((IDX == 250) XOR (IDX !=1) AND IDX>  270)

it now simplifies to:

((TRUE) XOR (TRUE) AND FALSE)

So what should be the solving order...

What language is this? What are operator precedence rules for this language?

For what it's worth, in C and C++ '!=' (not-equal could be used as a logical XOR) has a higher precedence than && (logical AND), so (true != true && false) is parsed as ((true != true) && false) and evaluates to false. On the other hand, '&' (bitwise AND) has a higher precedence than '^' (bitwise XOR), so (1 ^ 1 & 0) is parsed as (1 ^ (1 & 0)) and evaluates to 1.

Generally, when in doubt, add an extra pair of parentheses.

Igor Tandetnik

• 13 Maret 2012 21:47

This is why this table is useful. The table has the things higher in the table take precedence over the things lower in the table, and the bitwise and operator has a higher precedence than the bitwise exclusive or. (I think this table may be better because it groups them a bit better, where the MSDN documentation puts extra spaces between the groups, the other table actually uses an extra column). So because bitwise and is first, it will follow the operator precedence and parse it as

true ^ (true & false)

so always be aware of the operator precedence.

This is a signature

Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

• 13 Maret 2012 21:48

On 3/13/2012 5:44 PM, Igor Tandetnik wrote:

On 3/13/2012 5:29 PM, roberto wrote:

Consider the following:

((IDX == 250) XOR (IDX !=1) AND IDX>   270)

it now simplifies to:

((TRUE) XOR (TRUE) AND FALSE)

So what should be the solving order...

What language is this? What are operator precedence rules for this language?

The table at

http://en.wikipedia.org/wiki/Logical_connective#Order_of_precedence

lists AND as having higher precedence than EQU. It doesn't mention XOR, but one would think that XOR and EQU should be of the same precedence. If you take this common-practice definition, then your expression is (TRUE XOR (TRUE AND FALSE)) and evaluates to TRUE.

Igor Tandetnik

• 14 Maret 2012 1:45

Hi Igor,

This:

"((IDX == 250) XOR (IDX !=1) AND IDX>  270)"

is a character string which I pass into one of my functions which loads a selective set of records. Anyways, right now I am only using one field called "IDX" to test this stuff. But the objective is to eventually do stuff like this:

RST_OpenRSet(pRstObj, eRSET_LOAD, 0, "IDX >= 250 AND IDX <= 300 AND (((f_Name == Tom) AND f_Adress > 100) OR f_City == Toronto)";

I can't use C's file "OPEN"/"CLOSE" etc... commands cause as you know my platform is not a PC. So it is something I was forced to create. I know, I know... re-inventing the darn wheel again. But I really didn't think this was going to be so tough. At first I figured an afternoon and I am done.... ha!! Ha!! Ha!! I only wished. If you think about it, with a long query, there are hundereds of different combinations where brackets can be place amongs these expressions. Bracket analysing is so important that I under estimated it. So I created only 3 logical operators for now, they are: "AND", "OR", "XOR. The rest are comparitive operators, fields, constants and brackets. It's just I am not 100% sure about this one situation described below...

It gets sort of messy if the user wants to introduce a bracketed expression within another expression like this small example:

*[Assuming IDX is 250]*

"(((IDX == 250) XOR (IDX !=1 AND IDX != 250) OR (IDX > 50) OR IDX>  270) AND IDX < 249)"

So for example purposes, let's say the above expression comes down to the following:

(((TRUE)  XOR (FALSE) OR (TRUE) OR FALSE) AND FALSE)

You see, the latter line has two expressions that have their own brackets which are the 2nd and the 3rd from the left as so: "(FALSE) OR (TRUE)". The rest of the brackets are paired off. When my code sees something like this, it has to solve for the  "(FALSE) OR (TRUE)" part first, which would result in a "TRUE". And from this point on it gets a little shady ... let me explain .... as we now are left with:

(((TRUE) XOR (TRUE) OR FALSE) AND FALSE)

The first thing I am doing (and correct me if I am wrong) is I will solve for the inside part of the expression >>> (TRUE) OR FALSE) leaving us with:

(((TRUE) XOR (TRUE) AND FALSE)

and then I do:

(TRUE) XOR (TRUE)

and finally:

(FALSE)

As you can see I do this by solving any internal expressions first and then working right to left for every other expression.

I guess I was trying to ask you if it is okay to do it this way... I *think* it is but I am not 100% sure.

regards

r

• 14 Maret 2012 1:58

On 3/13/2012 9:45 PM, roberto wrote:

This:

"((IDX == 250) XOR (IDX !=1) AND IDX>   270)"

is a character string which I pass into one of my functions which loads a selective set of records.

You want something like this:

http://en.wikipedia.org/wiki/Shunting_yard_algorithm

For other approaches, see

http://en.wikipedia.org/wiki/Operator-precedence_parser

Igor Tandetnik

• 14 Maret 2012 3:05

wikipedia ! huh!

Jeeezze there's even a C sample!!!  Igor let me ask you something. I was wondering if out there on the Internet there are C samples I can include in my code and use as tools I need. For example C code that does spell checking ?? Something like a module of C code that has functions that take in a pointer to a string and returns a pointer to another string which holds the correct spelling of the word.

I looked in wikipedia but I could not find such a thing. Are there such tools out there and where can I look for them?

r

• 14 Maret 2012 3:11

On 3/13/2012 11:05 PM, roberto wrote:

For example C code that does spell checking ??

It's C++ though. I doubt anyone could be bothered to write something this complex in pure C.

Igor Tandetnik

• 14 Maret 2012 17:20

Hi Igor,

If Hunspell is a piece of C++ software I can always include the file(s) of my project in my C++ embedded compiler someday in the furture. But it seems hunspell is a package that we buy and is used as a spell checker for Google Chrome, and it is also used by proprietary softwares, like Mac OS X, memoQ, Opera. I could be wrong ! However it is good to know stuff like this exists.

Thanks

• Diedit oleh 14 Maret 2012 17:20
•
• 14 Maret 2012 17:47

Hi Igor,

This:

"((IDX == 250) XOR (IDX !=1) AND IDX>  270)"

is a character string which I pass into one of my functions which loads a selective set of records. Anyways, right now I am only using one field called "IDX" to test this stuff. But the objective is to eventually do stuff like this:

RST_OpenRSet(pRstObj, eRSET_LOAD, 0, "IDX >= 250 AND IDX <= 300 AND (((f_Name == Tom) AND f_Adress > 100) OR f_City == Toronto)";

Since your text is not an expression in a statement being compiled by the compiler, the real answer is that the precedence of the various operators, including the parentheses, is whatever your code decides it is in your function.

I have no doubt that you would like the precedence to be the same as C.  You might want to look at some compiler theory to help you write the evaluation portion of your function because you are in essence writing a special purpose compiler (or perhaps interpreter is a better word) for a subset of the language.

• Ditandai sebagai Jawaban oleh 14 Maret 2012 19:39
•
• 14 Maret 2012 17:54

On 3/14/2012 1:20 PM, roberto wrote:

But it seems hunspell is a package that we buy

Which part of "library  under GPL/LGPL/MPL tri-license" led you to believe that?

and is used as a spell checker for Google Chrome, and it is also used by proprietary softwares, like Mac OS X, memoQ, Opera.

How is this a bad thing?

Igor Tandetnik

• Ditandai sebagai Jawaban oleh 14 Maret 2012 19:39
•
• 14 Maret 2012 19:47

I don't know much about what that means. But are you saying that if my compiler accepts C++ I can download the hunspell for free and use it in my code?

>How is this a bad thing?

Never said it was a bad thing, I was debating weather or not I would be able to use it with my C++ compiler.

r

• 14 Maret 2012 20:03

On 3/14/2012 3:47 PM, roberto wrote:

I don't know much about what that means.

Then I recommend you consult with your lawyer.

But are you saying that if my compiler accepts C++ I can download the hunspell for free and use it in my code?

Igor Tandetnik

• 14 Maret 2012 20:34

Allright... thanks Igor!

r

• 15 Maret 2012 0:27

>For example C code that does spell checking ?

Also see:

GNU Aspell
http://aspell.net/

6.1 Through the C API
http://aspell.net/man-html/Through-the-C-API.html#Through-the-C-API

- Wayne
• 15 Maret 2012 3:50

At last.... Want scripting functionality? Free? Small? Use Lua.

-- pa