programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# how to create a boolean truth table

Ranch Hand
Posts: 38
I am rather confuse on truth tables or how to create them. Take for example the following code segement

okay this is something I need to understand for a college class. I took there code made a program out of it and added a,b,c,d as boolean so I could set them to true or false. But there has to be another way to figure this out and I am under the impression it has something to do with truth tables. Any help would be greatly appreciated....

Thanks,

Pete

Ranch Hand
Posts: 323
i'm not really sure what you mean by a "truth table". i've always thought that was a piece of paper with a grid on it. along the sides of the grid, you'd write in the values of two different variables (one for each side), and inside the grid squares, you'd write in whether a third variable (a boolean) should be "true" or "false" when the other two variables had the values corresponding to that square's coordinates.

what you've got here seems like a four-dimensional grid, where each dimension is a boolean variable (a through d), and the values in the grid squares would be the strings "1" through "6". the only real complication seems to be in the cases where the variable c is false, or d is true; that's your first big "if" block near the top. all other cases seem more straight-forward.

actually writing out your truth table would take, hm, four actual grids if you keep to one variable per axis. if you compress it a bit (more than one variable per axis) you could shrink the piece(s) of paper you'll need, at the cost of making it a bit harder to read. it might prove useful, though.
[ May 15, 2005: Message edited by: M Beck ]

(instanceof Sidekick)
Ranch Hand
Posts: 8791
I recall doing this in school with Karnaugh Maps and Gray Code. The combination allows you to find the minimum number of tests to cover a wide number of conditions. Google for them and see if you find anything interesting. HEre's one set of university notes that gets into it part way through:http://cms.brookes.ac.uk/modules/notes/258_u-lect5.ppt

I have some REXX code around from the 90s that will generate code (any language from templates) if you tell it which boxes are shaded in your Karnaugh map. Send private e-mail with your e-mail address if you're interested.

author and iconoclast
Sheriff
Posts: 24217
38
Ah, Karnaugh maps. Fondly remembering P.J. Plauger's old "Programming on Purpose" column in Computer Language magazine. Those were the days.

Ranch Hand
Posts: 1148
9
Well if you want to make a truth table, just put a for loop around your big if/elseif/else block of logic and set a,b,c,&d at the start of the loop. Your existing println() calls will fill in the last table column.

It's been my experience tha truth tables and Karnaugh maps were usually used when the result of your fuction was a boolean, so that they were just filled with T's and F's. In this case you could either make six separate tables, one for each possible out put, or you caould make three separate ones, one for each bit in the output integer.

Ryan

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
The "Programming on Purpose" columns were collected into a fantastic little book by the same name. Highly recommended for the state of the art of design ... right before objects. The essays on design styles like Top Down, Bottom Up, Left to Right, Right to Left, Inside Out and Outside In are timeless. I became parted from my copy. Hope those were right!

One word of warning on using this kind of logic reduction ... the resulting code can be nearly impossible to translate back into business terms. I used it a couple times and kept the graph as a comment.

Here's one input to my little code generator showing all the possible rectangles in a Karnaugh map up to 4 variables and what they imply ...

 It's exactly the same and completely different as this tiny ad: ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter https://coderanch.com/t/690611/ScroogeXHTML-RTF-HTML-XHTML-converter