Question

// Write a function that returns 0 if x is 0, returns -1 // if x...

// Write a function that returns 0 if x is 0, returns -1
// if x < 0, returns 1 if x > 0
// Your code must follow the Bit-Level Integer Coding Rules
// on the textbook (located between hw 2.60 and 2.61).
// You can assume w = 32.
// The only allowed operations in your code are:
// ! ~ & ^ | + << >>
// This requirement is more restrictive than the coding rules.

int sign(int x) {
return 0;
}

Homework Answers

Answer #1

CODE

// Write a function that returns 0 if x is 0, returns -1

// if x < 0, returns 1 if x > 0

// Your code must follow the Bit-Level Integer Coding Rules

// on the textbook (located between hw 2.60 and 2.61).

// You can assume w = 32.

// The only allowed operations in your code are:

// ! ~ & ^ | + << >>

// This requirement is more restrictive than the coding rules.

int sign(int x) {

    return 1 + (x >> 31) - (-x >> 31);

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
// Write a function that returns 0 if x is 0, returns -1 // if x...
// Write a function that returns 0 if x is 0, returns -1 // if x < 0, returns 1 if x > 0 // Your code must follow the Bit-Level Integer Coding Rules // on the textbook (located between hw 2.60 and 2.61). // You can assume w = 32. // The only allowed operations in your code are: // ! ~ & ^ | + << >> // This requirement is more restrictive than the coding rules. //...
C++ Write a function that returns a string of 1's and 0's. This needs to be...
C++ Write a function that returns a string of 1's and 0's. This needs to be the binary representation of a number. Do not use loops. Use only recursion. Do not change the function's parameters or add more parameters. Do not change the main program. #include #include string bin(int number) { //Code here } int main() {    cout << bin(43) << endl; // Should be 101011    return 0; }
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum...
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum of the digits of n. Use the following main function to test your code: int main() { int n, sn; cout << "Enter q to quit or an integer: "; while ( cin >> n ) { sn = sumofdigits(n); cout << "sumofdigits( " << n << " ) = " << sn; cout << "\nEnter q to quit or an integer: "; }...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1 if X is a prime number, or 1 if X is not a prime number. b) write a program that takes a positive integer N and prints all prime numbers from 2 to N by calling your function is_prime from part a.
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
write a function that returns the value of the binomial expansion for a positive (or zero)...
write a function that returns the value of the binomial expansion for a positive (or zero) integer power n and any valid value of x. first write a function that calculates the value of the rth term. This all has to be done without importing any packages in python. Current code, def binomial_term_L1(x,n): return (1+x)**n but having trouble getting the code for the rth term to work.
1) Write a function in C that takes in a single integer parameter and returns an...
1) Write a function in C that takes in a single integer parameter and returns an array of factors. The function signature should be : int* get_factors(int number) To make things a little easier, assume that the input (number) will never have more than 100 factors. The returned array should be null-terminated. 2) Write a main() function that asks a user for a number, calls the function above, then prints the results.
Write a function called TaylorSin.m that takes as input an array x, and positive integer N,...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N, and returns the Nth Taylor polynomial approximation of sin(x), centered at a = 0. The first line of your code should read function s = TaylorSin(x,N) HINT: in computing k!, use kfact = k*(k-1)*kfact since you are counting by 2
Write the code using C++ ( from the TEXTBOOK Data Structure using C++) Add a function...
Write the code using C++ ( from the TEXTBOOK Data Structure using C++) Add a function to the dateType class. The function's name is compareDates. Its prototype is int compareDates(const dateType& otherDate; The function returns -1 if otherDate is greater than than this date The function returns 0 if otherDate equals this date The function returns 1 if otherDate is less than this date Examples dateType d1(1, 1, 2019); dateType d2(11,1, 2019) d1.compareDates(d2) returns -1 d2.compareDates(d1) returns 1 d2.compareDates(d2) returns...
Consider the joint density function f (x, y) = 1 if 0<= x<= 1; 0<=y<= 1....
Consider the joint density function f (x, y) = 1 if 0<= x<= 1; 0<=y<= 1. [0 elsewhere] a) Obtain the probability density function of the v.a Z, where Z = X^2. b) Obtain the probability density function of v.a W, where W = X*Y^2. c) Obtain the joint density function of Z and W, that is, g (Z, W)