Considering the logic circuit shown in figure, for which input values, in A, B, C order, will the output D be true? Justify answer.
Thank you!
D will be 1 for the following inputs:
A=0, B=1, C=0 : D=1
A=1, B=0, C=0 : D=1
A=1, B=1, C=0 : D=1
A=0, B=0, C=0 : D=1
A=0, B=0, C=1 : D=1
Refer Basics of logic gates
Related
int64_t foo(int64_t a, int64_t b, int64_t c){
return a == b ? c : a == c ? b : a;
}
Is there any non-branching bitwise hack that implements the function above?
yes, the are:
return ((!(a-b))*(c^a))^((!(a-c))*(b^a))^a;
if a=b=c: (1*(c^a))^(1*(b^a))^a = a^b^c= a
if a=b != c: then (1*(c^a))^0^a= c^a^a= c
the same for a=c != b
if a != b != c: (0*(a^c))^(0*(a^b))^a = 0^0^a=a
it is questionable if this way is faster, but try it.
UB-Warning.
if a-b or a-c result in a over or underflow, it is undefined behavior. may use unsigned variables.
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
I'm unable to understand the working of this piece of code.
#include<stdio.h>
void main(){
int a,b;
a=3,1;
b=(5,4);
printf("%d",a+b);
}
The output is 7. What is that assignment?
Comma operator evaluates its first operand and discards the result, and then evaluates the second operand and returns this value.
After executing
a=3,1; // (a = 3), 1;
a will have 3 and after
b=(5,4); // Discard 5 and the value of the expression (5,4) will be 4
b will have 4.
Some more examples on Wikipedia:
// Examples: Descriptions: Values after line is evaluated:
int a=1, b=2, c=3, i=0; // commas act as separators in this line, not as an operator
// ... a=1, b=2, c=3, i=0
i = (a, b); // stores b into i
// ... a=1, b=2, c=3, i=2
i = a, b; // stores a into i. Equivalent to (i = a), b;
// ... a=1, b=2, c=3, i=1
i = (a += 2, a + b); // increases a by 2, then stores a+b = 3+2 into i
// ... a=3, b=2, c=3, i=5
i = a += 2, a + b; // increases a by 2, then stores a to i, and discards unused
// a + b rvalue. Equivalent to (i = (a += 2)), a + b;
// ... a=5, b=2, c=3, i=5
i = a, b, c; // stores a into i, discarding the unused b and c rvalues
// ... a=5, b=2, c=3, i=5
i = (a, b, c); // stores c into i, discarding the unused a and b rvalues
// ... a=5, b=2, c=3, i=3
return a=4, b=5, c=6; // returns 6, not 4, since comma operator sequence points
// following the keyword 'return' are considered a single
// expression evaluating to rvalue of final subexpression c=6
return 1, 2, 3; // returns 3, not 1, for same reason as previous example
return(1), 2, 3; // returns 3, not 1, still for same reason as above. This
// example works as it does because return is a keyword, not
// a function call. Even though most compilers will allow for
// the construct return(value), the parentheses are syntactic
// sugar that get stripped out without syntactic analysis
a=3,1; // (a=3),1 -- value of expression is 1, side effect is changing a to 3
b=(5,4);
printf("%d",a+b); // 3 + 4
In this statement
a=3,1;
two operators are used: the assignment operator and the comma operator. The priority of the assignment operator is greater than the priority of the comma operator, thus this statement is equivalent to
( a = 3 ), 1;
1 is simply discarded, so a is assigned the value 3.
In this statement
b=(5,4);
due to the parentheses the comma operator is evaluated first. Its value is the value of the last expression, 4. So b is assigned the value 4.
As result you get a + b => 3 + 4 which equals 7.
I am writing a program to demonstrate the bisection algorithm(from numerical methods).
What I am doing is this:
defined a function F(int), which takes the integer and returns the value of the polynomial at that integer.
In the bisection() function:
a) a,b are the initial approximations.
b) the for() construct finds the value of a after which the sign(magnitude) of F(a) changes.
c) the printfs are for troubleshooting purposes(except for the last one).
d) int prec is for the precision required for the solution (no. of iterations), where no. of iterations, N=log((b-a)/E), E=10^(-prec).
What I'm getting is this:
Output:
a=1 F(a)=-0.207936
a=1, b=2
Approximation 0: x = 0 a=1072693248 b=1
Approximation 1: x = 0 a=1072693248 b=1
Approximation 2: x = 0 a=1072693248 b=1
Approximation 3: x = 0 a=1072693248 b=1
Approximation 4: x = 0 a=1072693248 b=1
The solution is: x = 1.000000
I have tried commenting the N=... statement and assigning constant integer to N, but to no effect.
Interestingly, commenting out all statements with variable x in the for() construct does not alter values of a and b.
Oddly, if the statement x=(a+b)/2; is commented out, then the value of a is affected by the value with which x is initialized:
i) a=1072693248, when x=1,
ii)when x=0, a=0,b=0
iii)when x=-1, a=-1074790400, b=1
I am using Microsoft Visual C++ 2010 Express for compilation.
Please help me out.
Here is the code:
#include<stdio.h>
#include<math.h>
#define MIN -10
double F(int x)
{
double y=(x*x)-(2*x)+0.792064;
// printf("\nx=%d y=%d",x,y);
return y;
}
void bisection(int prec)
{
int a,b=0;
int c,N=10,i;
double x=0;
for(a=MIN;(F(a)*F(a-1))>0;a++);
printf("\na=%d F(a)=%f",a,F(a));
b=a+1;
printf("\n\na=%d, b=%d",a,b);
N=(log((float)(b-a))+5);
for(i=0;i<N;i++)
{
x=(a+b)/2;
printf("\nApproximation %d: x = %d\ta=%d b=%d",i,x,a,b);
if((F(a)*F(x)>0))
a=x;
else
b=x;
}
printf("\n\nThe solution is: x = %f",x);
getchar();
}
int main()
{
bisection(4);
return 0;
}
It's because a and b are declared as ints. When you write x = (a + b) / 2;, all the elements on the right side are integers, so they will be evaluated to an integer. Moreover, as the difference of a and b is 1, the value would be equal to the smaller one of them, which is a. That's why the value of x becomes 1.0. To fix it, the upper and lower limits should be declared as doubles, not ints. When a and b are integers, their values cannot converge to the actual value of x.
Also, you're using %d in printf to write a double value. It should be %lf.
designing a pushdown automata for the language a^n b c^n+2, n>0
I have been asked to implement the automata for the above language .. please help?
I tried popping a 2 (c)s everytime I push an (a) on to the stack but it seems not to work with odd number of (a)s ....
You must process the a's in the normal way, ie, for each to read from the tape you stack A, until you finish reading the a's, if you read a b, leave the top of the stack as it is, finally you must process all C's. The transition function is:
(q0, a, Z) = (q0, AZ)
(q0, a, A) = (q0, AA)
(q0, b, A) = (q1, A)
(q1, c, A) = (q1, epsilon) (until the amount of a's are equal to the amount of c's)
(q1, c, Z)= (q2, Z) (read the first extra c)
(q2, c, Z)= (q3, Z) (read the second extra c)
(q3, epsilon, Z)= (qf, Z) (qf is the final state)
The graphic representation of the PDA is:
I'm implementing a computer simulator in C with the challenge not to use conditionals (ie no if/else, switch/case, while/for, etc.). There are a lot of muxes in the hardware I'm simulating, so it would be nice if I could use the conditional ternary logic operators. So, my question: do C compilers create MUX logic from ternary logic operators, or do they create branches?
Example:
int a, b, q, r;
/* Ternary logic */
r = q ? a : b;
/* MUX equivalent logic */
r = ( q & a ) | ( (~q) & b )
/* Branch equivalent logic */
if (q) r = a; else r = b;
The ternary operator is equivalent to a branch: i.e. the value not returned is not evaluated.
I don't know how much you are constrained, so note that boolean operators && and || don't evaluate their second argument is the result can be determined from the first.
(And note that you line "MUX" doesn't something very different from the other two expressions: it select bits from a or b depending on the value of corresponding bit in q, it doesn't select a or b depending if q is null or not; Edit: it is worse: you are using !q and not ~q...).
C compilers create branches out of ternary statements.
Using Freescale's Codewarrior IDE, I compiled the following C program:
int a, b, q, r;
void main(void) {
a = 0;
b = 1;
q = 0;
r = q ? a : b;
..
..
}
The assembly corresponding to the ternary statement is as follows:
...
LDX 0x1104 ; load val[q] into register x
BNE *+4 ; branch to abs addr 0xC016 if val[q]==a
BRA *+5 ; branch to abs addr 0xC019 if val[q]!=a
...
Often, branching logic is created, but is very possible to do ternary operation without any kind of branch if you're fine with both values being evaluated. Consider this:
#include <stdio.h>
static inline int isel(int cond, int a, int b)
{
int mask = cond | (-cond);
mask >>= 31;
return (b & mask) | (a & ~mask);
}
int main(void)
{
printf("1 ? 3 : 4 => %d\n", isel(1, 3, 4));
printf("0 ? 3 : 4 => %d\n", isel(0, 3, 4));
printf("-1 ? 3 : 4 => %d\n", isel(-1, 3, 4));
return 0;
}
This code assumes that right-shifting of signed numbers will sign-extend, and that sizeof(int) == 4.
Some processors can do isel() as an assembly instruction. However, both values will be evaluated, which the ternary ?: doesn't do.
Depending on the situation, the compiler will generally try to do the fastest thing, either branching to avoid redundant calculation, or doing this kind of logic if values in the ternary expression are simple.