|
|
|
 |
|
Truth Seeker Show the Truth
Truth Seeker
Searchs for boolean expressions for a set of results in the truth table.
Sometimes you may have a long boolean expression in your code whose variables are repeated several times. This type of boolean expression will be more efficient and elegant if could be reduced or simplified to a simpler boolean expression which gives the same result.
For example, the long boolean expression
result = a & (b | c) ^ a & ~b | ~c ;
has variables a, b, and c repeated 2 times each, so we could expect to replace it by a simpler boolean expression such as
result = a & b | ~c ;
While Karnaugh map (aka Veitch diagram) would help you understanding your expression well, this tool might help you save your time if you're lucky :). Firstly, construct the truth table for your boolean expression using Show the Truth, and search the simpler one for your result below.
Note that the precedence of bitwise operators in javascript is ~, &, ^, |. Therefore the expression a|~b&c^d would mean a|(((~b)&c))^d) or logically a || (((!b) && c) XOR d) etc. However javascript doesn't support logical XOR, so you could simulate it with a XOR b = (a || b) && !(a && b).
Example
I had written this tool when worked on another much simpler tool, HTML Encoder. In the heart of encoding function it will check if a character has an entry in the HTML code table, return it if any or else the character itself. Moreover if the character is space, user can choose either to encode it or not. This needs the following branch to do the job:
return spc ? (html ? html : c) : (c==' ' ? c : (html ? html : c)) ;
This branch can be thought as boolean expression with the truth table something like
| spc | html | c==' ' | result | |
| 1 | 1 | 1 | 1 | (html) |
| 1 | 1 | 0 | 1 | (html) |
| 1 | 0 | 1 | 0 | (c) |
| 1 | 0 | 0 | 0 | (c) |
| 0 | 1 | 1 | 0 | (c) |
| 0 | 1 | 0 | 1 | (html) |
| 0 | 0 | 1 | 0 | (c) |
| | 0 | 0 | 0 | 0 | (c) |
Now I want to replace the branch above with the simpler boolean expression which gives the exact same result. All I have to do is searching for the result '11000100' with 3 variables. I got no result when the expression length was set to 3, but with the length of 4 the following expressions are found:
(b & c & ~a) ^ b
(b & ~c) | (a & b)
Since javascript doesn't have logical XOR I used the last expression to replace my branch as below:
return ((html && !(c==' ')) | (spc && html)) ? html : c ;
Most of the visitors who used this tool also used IP - 国 (16.7%), ビーワイズビット (16.7%), Source (16.7%) |
|