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.
// You can not use if else.
int sign(int x) {
return 0;
}

Homework Answers

Answer #1

#include <stdio.h>

int sign(int x) {

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

}

int main()
{
int x;
printf("Please enter x: \n");
scanf("%d",&x);
int a = sign(x);
printf("returned value is: %d", a);
return a;
}

The logic what i wrote above for sign function is:

The shift operator converts every value which is less than 0 to -1. Every other number to 0,

When we do -x>>31 with x is positive number then it return -1.

But when we do for 0 then n>>31 and -n>>31 both returns 0.

So from above conclusion the formula that created is:

0 + (x >> 31) - (- x>>31)

Output:

1 )

Please enter x:

15

returned value is: 1

2)

Please enter x:

0

returned value is: 0

3)

Please enter x: -50

returned value is: -1

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
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; }
USING JAVA: I was asked to write a function that obtains a name from the user...
USING JAVA: I was asked to write a function that obtains a name from the user and returns it in reverse order (So if the user inputs "MIKE" the function returns "EKIM"). You can't use string variables it can only be done using a Char Array. Additionally, you can use a temporary Char Array but you are supposed to return the reverse order in the same char array that the user input, this is for hypothetical cost purposes -we are...
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
The function named myRandomNum that returns a random integer in the range [1, 100] is defined...
The function named myRandomNum that returns a random integer in the range [1, 100] is defined as follows: int myRandomNum () { return rand()%100 + 1; } Now, write a program (code fragment) that repeatedly generates and prints random integers in the range [1, 100]. The program must stop when the absolute value of the difference between successive printed values is less than 5. Two sample output is given to help you understand the problem: 1. The program can possibly...
Find a function f(x)  satisfying f ' '(x) = sqrt{x}, f ' (1)=0 and f(1)=0. You must...
Find a function f(x)  satisfying f ' '(x) = sqrt{x}, f ' (1)=0 and f(1)=0. You must explain in words what you are doing so that someone who knows what antidifferentiation is but otherwise does not have experience and can follow your computations. Give your final answer in what seems to you to be the simplest form.
Find a function f(x)  satisfying f ' '(x) = sqrt{x}, f ' (1)=0 and f(1)=0. You must...
Find a function f(x)  satisfying f ' '(x) = sqrt{x}, f ' (1)=0 and f(1)=0. You must explain in words what you are doing so that someone who knows what antidifferentiation is but otherwise does not have experience can follow your computations. Give your final answer in what seems to you to be the simplest form. Use antidifferentiation, not integration.
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an n-by-n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note that...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT