How to simplify boolean function into two logic gates? - boolean

Can anyone help me to simplify this boolean function into two logic gates?
C(out) = AC(in) + BC(in) + AB

This expression represents what is commonly known as a three input majority gate - the output is TRUE only when the majority of inputs are true (2 or 3 inputs must be true for the 3 input case). In general it takes 4 basic logic gates to implement this (5 if you you are restricted to 2-input gates).
If you Google for "majority gate" you will find a variety of implementations, e.g. on this page I found the following, which I think matches your criteria (other than the unfeasible requirement of doing it with only 2 gates):

About majority function with n boolean variables.
for n variables, f(x1,x2,...xn) there will be total nC[n/2] terms for OR operation. Each term contains [n/2] variables for AND operation.
ex: f(00111)= OR{ and(0,0,1) and(0,0,1) and(0,0,1) and(0,1,1) and(,0,1,1) and(0,1,1,) and(0,1,1) and (0,1,1,) and(0,1,1,) and(1,1,1 )
=0 OR 0 OR 0 OR...... OR 1=1=majority of ones is true.

Related

Counting the number of true predicates and limiting

Is there a specific way I can limit the number of true predicates available using a specified fact?
At the moment I have total(2). as a fact.
I thought this would work:
:- total(N), #count{x:something_to_limit(x)} = K, K=N.
However this doesn't limit the number of something_to_limit predicates to the specified total(2) fact where N would equal 2.
Any help would be greatly appreciated:)
The x in x:something_to_limit(x) is a constant symbol, you probably want to use variables X. The constraint
:- total(N), #count{X:something_to_limit(X)} = K, K=N.
should work.

Differences in optimization statement syntax (clingo 3 and clingo 4)

I have an optimization statement in a logic program, for clingo3:
#minimize [ batteryFlat(mycar)=1, batteryFlat(yourcar)=1, hasNoFuel(mycar)=1,
hasNoFuel(yourcar)=1, brokenIndicator(mycar)=1, brokenIndicator(yourcar)=1].
(Basically, I want the solution to contain as few of the above as possible - they are all of equal weight).
This syntax works for clingo3, but not clingo4. How should it be re-written for clingo4?
How about this:
#minimize {batteryFlat(mycar); batteryFlat(yourcar); hasNoFuel(mycar);
hasNoFuel(yourcar); brokenIndicator(mycar); brokenIndicator(yourcar)}.
The set is now separated with ; and you can then use , to conjoin conditions. Each element has the same priority, but if you want different priorities you can do something like:
#minimize {1#1: batteryFlat(mycar); 1#2: batteryFlat(yourcar); hasNoFuel(mycar);
hasNoFuel(yourcar); brokenIndicator(mycar); brokenIndicator(yourcar)}.
Now the first atom has priority one (for at least one occurrence, I think) and the second atom a higher priority.
Or, if you have variables give priority to the number of different groundings like so:
#minimize {X#1: batteryFlat(X); 1#2: batteryFlat(yourcar); hasNoFuel(mycar);
hasNoFuel(yourcar); brokenIndicator(mycar); brokenIndicator(yourcar)}.
Comparisons are shown here: http://sourceforge.net/projects/potassco/files/clingo/4.2.0/

COBOL add 0 to a Variable in COMPUTE

I ran into a strange statement when working on a COBOL program from $WORK.
We have a paragraph that is opening a cursor (from DB2), and the looping over it until it hits an EOT (in pseudo code):
... working storage ...
01 I PIC S9(9) COMP VALUE ZEROS.
01 WS-SUB PIC S9(4) COMP VALUE 0.
... code area ...
PARA-ONE.
PERFORM OPEN-CURSOR
PERFORM FETCH-CURSOR
PERFORM VARYING I FROM 1 BY 1 UNTIL SQLCODE = DB2EOT
do stuff here...
END-PERFORM
COMPUTE WS-SUB = I + 0
PERFORM CLOSE-CURSOR
... do another loop using WS-SUB ...
I'm wondering why that COMPUTE WS-SUB = I + 0 line is there. My understanding is that I will always at least be 1, because of the perform block above it (i.e., even if there is an EOT to start with, I will be set to one on that initial iteration).
Is that COMPUTE line even needed? Is it doing some implicit casting that I'm not aware of? Why would it be there? Why wouldn't you just MOVE I TO WS-SUB?
Call it stupid, but with some compilers (with the correct options in effect), given
01 SIGNED-NUMBER PIC S99 COMP-5 VALUE -1.
01 UNSIGNED-NUMBER PIC 99 COMP-5.
...
MOVE SIGNED-NUMBER TO UNSIGNED-NUMBER
DISPLAY UNSIGNED-NUMBER
results in: 255. But...
COMPUTE UNSIGNED-NUMBER = SIGNED-NUMBER + ZERO
results in: 1 (unsigned)
So to answer your question, this could be classified as a technique used cast signed numbers into unsigned numbers. However, in the code example you gave it makes no sense at all.
Note that the definition of "I" was (likely) coded by one programmer and of WS-SUB by another (naming is different, VALUE clause is different for same purpose).
Programmer 2 looks like "old school": PIC S9(4), signed and taking up all the digits which "fit" in a half-word. The S9(9) is probably "far over the top" as per range of possible values, but such things concern Programmer 1 not at all.
Probably Programmer 2 had concerns about using an S9(9) COMP for something requiring (perhaps many) fewer than 9999 "things". "I'll be 'efficient' without changing the existing code". It seems to me unlikely that the field was ever defined as unsigned.
A COMP/COMP-4 with nine digits does have a performance penalty when used for calculations. Try "ADD 1" to a 9(9) and a 9(8) and a 9(10) and compare the generated code. If you can have nine digits, define with 9(10), otherwise 9(8), if you need a fullword.
Programmer 2 knows something of this.
The COMPUTE with + 0 is probably deliberate. Why did Programmer 2 use the COMPUTE like that (the original question)?
Now it is going to get complicated.
There are two "types" of "binary" fields on the Mainframe: those which will contain values limited by the PICture clause (USAGE BINARY, COMP and COMP-4); those which contain values limited by the field size (USAGE COMP-5).
With BINARY/COMP/COMP-4, the size of the field is determined from the PICture, and so are the values that can be held. PIC 9(4) is a halfword, with a maxiumum value of 9999. PIC S9(4) a halfword with values -9999 through +9999.
With COMP-5 (Native Binary), the PICture just determines the size of the field, all the bits of the field are relevant for the value of the field. PIC 9(1) to 9(4) define halfwords, pic 9(5) to 9(9) define fullwords, and 9(10) to 9(18) define doublewords. PIC 9(1) can hold a maximum of 65535, S9(1) -32,768 through +32,767.
All well and good. Then there is compiler option TRUNC. This has three options. STD, the default, BIN and OPT.
BIN can be considered to have the most far-reaching affect. BIN makes BINARY/COMP/COMP-4 behave like COMP-5. Everything becomes, in effect, COMP-5. PICtures for binary fields are ignored, except in determining the size of the field (and, curiously, with ON SIZE ERROR, which "errors" when the maxima according to the PICture are exceeded). Native Binary, in IBM Enterprise Cobol, generates, in the main, though not exclusively, the "slowest" code. Truncation is to field size (halfword, fullword, doubleword).
STD, the default, is "standard" truncation. This truncates to "PICture". It is therefore a "decimal" truncation.
OPT is for "performance". With OPT, the compiler truncates in whatever way is the most "performant" for a particular "code sequence". This can mean intermediate values and final values may have "bits set" which are "outside of the range" of the PICture. However, when used as a source, a binary field will always only reflect the value specified by the PICture, even if there are "excess" bits set.
It is important when using OPT that all binary fields "conform to PICture" meaning that code must never rely on bits which are set outside the PICture definition.
Note: Even though OPT has been used, the OPTimizer (OPT(STD) or OPT(FULL)) can still provide further optimisations.
This is all well and good.
However, a "pickle" can readily ensue if you "mix" TRUNC options, or if the binary definition in a CALLing program is not the same as in the CALLed program. The "mix" can occur if modules within the same run-unit are compiled with different TRUNC options, or if a binary field on a file is written with one TRUNC option and later read with another.
Now, I suspect Programmer 2 encountered something like this: Either, with TRUNC(OPT) they noticed "excess bits" in a field and thought there was a need to deal with them, or, through the "mix" of options in a run-unit or "across file usage" they noticed "excess bits" where there would be a need to do something about it (which was to "remove the mix").
Programmer 2 developed the COMPUTE A = B + 0 to "deal" with a particular problem (perceived or actual) and then applied it generally to their work.
This is a "guess", or, better, a "rationalisation" which works with the known information.
It is a "fake" fix. There was either no problem (the normal way that TRUNC(OPT) works) or the correct resolution was "normalisation" of the TRUNC option across modules/file use.
I do not want loads of people now rushing off and putting COMPUTE A = B + 0 in their code. For a start, they don't know why they are doing it. For a continuation it is the wrong thing to do.
Of course, do not just remove the "+ 0" from any of these that you find. If there is a "mix" of TRUNCs, a program may stop "working".
There is one situation in which I have used "ADD ZERO" for a BINARY/COMP/COMP-4. This is in a "Mickey Mouse" program, a program with no purpose but to try something out. Here I've used it as a method to "trick" the optimizer, as otherwise the optimizer could see unchanging values so would generate code to use literal results as all values were known at compile time. (A perhaps "neater" and more flexible way to do this which I picked up from PhilinOxford, is to use ACCEPT for the field). This is not the case, for certain, with the code in question.
I wonder if a testing version of the sources ever had
COMPUTE WS-SUB = I + 0
ON SIZE ERROR
DISPLAY "WS-SUB overflow"
STOP RUN
END-COMPUTE
with the range test discarded when the developer was satisfied and cleaning up? MOVE doesn't allow declarative SIZE statements. That's as much of a reason as I could see. Or perhaps developer habit of using COMPUTE to move, as a subtle reminder to question the need for defensive code at every step? And perhaps not knowing, as Joe pointed out, the SIZE clause would be just as effective without the + 0? Or a maintainer struggled with off by one errors and there was a corrective change from 1 to 0 after testing?

How to limit the number of times a rule can repeat in XTEXT?

My language has certain keywords which only accept values of certain length range (say, between 5 and 10 decimal numbers). This id correct:
KeyWord = 01234
This is incorrect:
KeyWord = 1234
I have a rule;
KeyWord:
'KeyWord' '=' INT+;
How to limit the number of times INT can repeat? This would so much easier if it was a more regexp-like syntax
I would implement this as a validation check instead of trying to fit this in the grammar itself. See http://www.eclipse.org/Xtext/documentation/2_1_0/050-validation.php
This will result in better error recovery and better error messages. It even allows quick-fixes.

What uses can you think of for Perl 6's junctions?

More information from the Perl 6 Wikipedia entry
Junctions
Perl 6 introduces the concept of junctions: values that are composites of other values.[24] In the earliest days of Perl 6's design, these were called "superpositions", by analogy to the concept in quantum physics of quantum superpositions — waveforms that can simultaneously occupy several states until observation "collapses" them. A Perl 5 module released in 2000 by Damian Conway called Quantum::Superpositions[25] provided an initial proof of concept. While at first, such superpositional values seemed like merely a programmatic curiosity, over time their utility and intuitiveness became widely recognized, and junctions now occupy a central place in Perl 6's design.
In their simplest form, junctions are created by combining a set of values with junctive operators:
my $any_even_digit = 0|2|4|6|8; # any(0, 2, 4, 6, 8)
my $all_odd_digits = 1&3&5&7&9; # all(1, 3, 5, 7, 9)
| indicates a value which is equal to either its left or right-hand arguments. & indicates a value which is equal to both its left and right-hand arguments. These values can be used in any code that would use a normal value. Operations performed on a junction act on all members of the junction equally, and combine according to the junctive operator. So, ("apple"|"banana") ~ "s" would yield "apples"|"bananas". In comparisons, junctions return a single true or false result for the comparison. "any" junctions return true if the comparison is true for any one of the elements of the junction. "all" junctions return true if the comparison is true for all of the elements of the junction.
Junctions can also be used to more richly augment the type system by introducing a style of generic programming that is constrained to junctions of types:
sub get_tint ( RGB_Color|CMYK_Color $color, num $opacity) { ... }
sub store_record (Record&Storable $rec) { ... }
How many days are in a given month?
given( $month ){
when any(qw'1 3 5 7 8 10 12') {
$day = 31
}
when any(qw'4 6 9 11') {
$day = 30
}
when 2 {
$day = 29
}
}
The most attractive feature of junctions is that you don't need to write a lot of code test for complex situations. You describe the situation with the junctions, then apply the test. You don't think about how you get the answer (for instance, using short circuit operators or if blocks) but what question you are asking.
Autothreading sounds cool, although I don't know what its current status is.
for all(#files) -> $file {
do_something($file);
}
Junctions have no order, so the VM is free to spawn a thread for every element in #files and process them all in parallel.