Question

In: Computer Science

C code required /* * isGreater - if x > y then return 1, else return...

C code required

/*
* isGreater - if x > y then return 1, else return 0
* Example: isGreater(4,5) = 0, isGreater(5,4) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/

Solutions

Expert Solution

#include <stdio.h>


int isGreater(int x, int y) {

int negateAdd = ~x + 1;

int addSum = y + negateAdd;

addSum = addSum >> 31;

return addSum&1;

}

int main(void) {

printf("%d\n", isGreater(4, 5));

printf("%d\n", isGreater(5, 4));

printf("%d\n", isGreater(-5, 6));

printf("%d\n", isGreater(6, -5));

printf("%d\n", isGreater(-4, -5));

printf("%d\n", isGreater(15, 4));

printf("%d\n", isGreater(5, -6));

return 0;

}

===========================================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

Consider this code snippet: if (x < 512) { y = 73; } else { y...
Consider this code snippet: if (x < 512) { y = 73; } else { y = 42; } Which of these goto-style code snippets is equivalent to that if/else code? Consider this code snippet: if (x < 512) { y = 73; } else { y = 42; } Which of these goto-style code snippets is equivalent to that if/else code? a. if (x >= 512) goto Label1; y = 73; goto Label2; Label1: y = 42; Label2: b....
Consider the following C code: long fact(long x) { if (x <= 1) { return 1;...
Consider the following C code: long fact(long x) { if (x <= 1) { return 1; } long px = x; long fx = fact(x - 1); return px * fx; } Copy the above C code into a C file. For each line of C code responsible for a push or pop operation (either directly or as a result of a call or return), add a comment describing the stack operation and its purpose. Hint: try compiling the C...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message based on the logic below. A buyer can pay immediately or be billed. If paid immediately, then display "a 5% discount is applied." If billed, then display "a 2% discount is applied" if it is paid in 30 days. If between 30 and 60 days, display "there is no discount." If over 60 days, then display "a 3% surcharge is added to the bill."...
Suppose f(x,y)=(1/8)(6-x-y) for 0<x<2 and 2<y<4. If all else is the same, then why can’t x...
Suppose f(x,y)=(1/8)(6-x-y) for 0<x<2 and 2<y<4. If all else is the same, then why can’t x be defined on the range [0,3]? Find p(0.5 < X < 1, 2 < Y < 3) Find fX(x) and fY(y) Are X and Y independent? Why or why not? Find p(0.5 < X < 1) and p(2 < Y < 3) Find p(Y<3|X=1) Find p(Y<3|0.5<X<1)
The joint PDF of X and Y is given by f(x, y) = C, (0< x<y<1)....
The joint PDF of X and Y is given by f(x, y) = C, (0< x<y<1). a) Determine the value of C b) Determine the marginal distribution of X and compute E(X) and Var(X) c) Determine the marginal distribution of Y and compute E(Y) and Var(Y) d) Compute the correlation coefficient between X and Y
f(x,y)=3(x+y) 0<x+y<1, 0<x<1, 0<y<1 (a) E(xy|x)=? (b) Cov(x,y)=? (c) x and y is independent? thank you!
f(x,y)=3(x+y) 0<x+y<1, 0<x<1, 0<y<1 (a) E(xy|x)=? (b) Cov(x,y)=? (c) x and y is independent? thank you!
X=5,Y=7,Z=10 A. if(X<Y): print "LESS" else: print "OTHER" B. if (x == 1): print "ONE" elif...
X=5,Y=7,Z=10 A. if(X<Y): print "LESS" else: print "OTHER" B. if (x == 1): print "ONE" elif (x == 2): print "TWO" elif (x == 3): print "THREE" elif (x == 4): print "FOUR" elif (x == 5): print "FIVE" elif (x == 6): print "SIX" else: print "OTHER" C. if (X<Z): print X X = X + 1 D. while (X<Z): print X X = X + 1 Q11. What is the final value of X in D Q12. Is...
C code only, not C++ or anything else // This program accepts the chest size from...
C code only, not C++ or anything else // This program accepts the chest size from the customer // and returns the proper shirt size as well as the price. // function prototype void check_size_price(int size_val, char *size_char, int *price); #include <stdio.h> int main() { int chest_size; //input - entered chest size char size_str; //output - size (S/M/L) int p; //output - price // validation loop for the input – the chest size must be a positive integer // Call...
Write the code to return the output in Rstudio. What is the code? Code: x <-...
Write the code to return the output in Rstudio. What is the code? Code: x <- c(28, 69, 5, 88, 19, 20) Output must be: [1] 4 2 1 6 5 3
Let f (x, y) = c, 0 ≤ y ≤ 4, y ≤ x ≤ y...
Let f (x, y) = c, 0 ≤ y ≤ 4, y ≤ x ≤ y + 1,  be the joint pdf of X and Y. (a) (3 pts) Find c and sketch the region for which f (x, y) > 0. (b) (3 pts) Find fX(x), the marginal pdf of X. (c) (3 pts) Find fY(y), the marginal pdf of Y. (d) (3 pts) Find P(X ≤ 3 − Y). (e) (4 pts) E(X) and Var(X). (f) (4 pts) E(Y)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT