*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
*You can assume w = 32.
*The only allowed operations in your code are: ! ~ & ^ | + << >>
*This requirement is more restrictive than the coding rules.
*You cannot use if-else statement, switch, loops, and comparison operators( <,>)
int sign(int x)
{
return 0;
}
Code:
#include<stdio.h>
int sign (int x)
//sign function which accepts the parameter
number x
{
return 0+(x >> 31)-(-x >> 31);
//logic to return if x>0 then 1 x<0 then -1 x=0 then 0
//the main logic behind this is first right shift the
given num to 31 bits that means for any negative numbsnier right
shift to 31 bits gives the value as -1.
//for all other numbers the value is zero.
}
int main()
{
int n;
printf("Enter the Number:\n");
//asking user to enter the
number
scanf("%d",&n);
//taking number from
user
printf("%d",sign(n));
//calling sign() function by passing number n as
argument and printing returned value
return 0;
}
Code and Outputs Screenshots:
Trace each Test Case:
For input number 189 the return value is 1 because 0+(189 >> 31) - (-189 >> 31) so here 189 >> 31 gives you 0 and -189>>31 gives you -1 so so -(-1) will turn to 1.
For input number -190 the return value is -1 because 0+(-190 >> 31) -(-(190) >> 31) so here -190 >> 31 gives you -1 and 190 >> 31 gives the value 0 so the final answer is 0-1-0=-1.
For input number 0 the return value is 0 because 0+ (0 >> 31) -(-0 >>31)=0+0+0=0.
Note : if you have any queries please post a comment thanks a lot..always available to help you..
Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x. Assume w=32. * For example 0xFF00 -> 0x8000, and 0x6600 --> 0x4000. * If x = 0,then return 0. */ int leftmost_one(unsigned x); Your function should follow the above bit-level integer coding rules, except that you may assume that data type int has w=32 bits. Your code should contain a total of at most 15 arithmetic, bit-wise, and logical operations. In C++ and has...
Write code for a function with the following prototype: // Divide by power of two. Assume 0 <= k < w-1 int divide_power2(int x, int k); The function should compute x/2 k with correct rounding (round toward 0), and it should follow the bit-level integer coding rules: • Assumptions – Integers are represented in twos-complement form. – Right shifts of signed data are performed arithmetically. – Data type int is w bits long. For some of the problems, you will...
Write a C function that takes a long int y as argument as well as two integers n and m, the function should swap byte i and j of a long int y (64-Bit Integer).and returns a long int. long int swapBytes(long int y, int i, int j) Rules: Not allowed to use division, multiplication, or modulus, relative comparison (<, >, <=, >=), loops, switches, function calls, macros, conditionals (if or ?:) ALLOWED to use all bit level and logic...
using C: Write code for a function with the following prototype: /* * Mask with least signficant n bits set to 1 * Examples: n = 6 --> 0x3F, n = 17 --> 0x1FFFF * Assume 1 <= n <= w */ int lower_one_mask(int n); Your function should follow the bit-level integer coding rules. Be careful of the case n =w.
/* * Return 1 if ptr points to an element within the specified intArray, 0 otherwise. * Pointing anywhere in the array is fair game, ptr does not have to * point to the beginning of an element. Check the spec for examples if you are * confused about what this method is determining. * size is the size of intArray in number of ints. Can assume size != 0. * Operators / and % and loops are NOT allowed....
a) Make a function which returns x/3 with a given integer x. double third(int x) { //Complete your code below. ______________________________________ return y; } b) Make a program which shows 2 ∗ n with a given user input n. #include<iostream> using namespace std; // Complete the blank. _______________________ int main(){ int n; cout<<"Enter:"; cin>>n; cout<<twice(n); return 0; } int twice(int x){ return 2*x; } c) Make a function which returns true if n is positive, otherwise returns false. ________ positive(int...
PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...
Hey i have a project due, and for the life of me i cannot figure out how to do this with the restrictions that are put in place, shiftwise operators such as << and >> <<<< >>>> cannot be used This is the question that follows: Shifter Implement a 32-bit shifter. Input: x (32 bits), c (5 bits), and op (2 bits). Output: y. This shifter should shift the input number x by c bits according to the shift operations...
Write a program that write a value-returning function, isVowel, that returns the value true if a given character is a vowel and otherwise returns false. You must insert the following comments at the beginning of your program and write our commands in the middle: Write a C++ Program: /* // Name: Your Name // ID: Your ID */ using namespace std; #include <iostream> using namespace std; bool isVowel(char ch); int main() { char ch; … … ...
Using lenguague C E5.5 (página 239) Write a function setbits(int x.char p.char n) that returns x with the n bits that begin at position p inverted (ie 1 changed to O and vice versa). Consider the position 0 the least significant bit of the variable x (remember that x is an integer)