Question

/* logicalShift - shift x to the right by n, using a logical shift can assume...

/*

logicalShift - shift x to the right by n, using a logical shift

can assume that 0<= n <=31

Examples: logicalShift(0x87654321,4) = 0x08675432

Legal ops: ! ~ & ^ | + << >>

Max ops: 20

Rating: 3    */

int logicalShift(int x, int n) {

return 0;

}

How would I code the logicalShift in C?

I have this, but this isn't right. I think I'm in the right track though:

int logicalShift(int x, int n) {

int a = ~0;

int shift = 31 + ((~n)+1);

a=a<<shift;

a=~a;

x=x>>n;

return x & a;

}

Homework Answers

Answer #1

int right_logical_shift(int num, int n){
int size_int = 8 * sizeof(int); // Calculating size of int in bits so as to create a bit mask for getting sign bit
int mask = 1<<(size_int - 1); // Creating a mask of the form 0x80000000 (32 bit int)
int sign_bit = (mask&num) == 0?0:1; // Sign bit = 0 if positive number else sign bit = 1
int num_without_sign = num&(~mask); // Removing sign bit from the number ~mask = 0x7FFFFFFF (32 bit int)
num_without_sign>>=n; // Right shift number without sign by n bits
sign_bit <<=(size_int - 1 - n); // Now left shift sign bit (which is either 0 or 1) by size of int - n - 1
num = (sign_bit|num_without_sign); // Logical And operation of sign bit and number without sign gives the
// number logically shifted by n bits
return num;
}

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
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;...
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;     for (i = 0; i < 32; i+=2)        if ((x & (1<          return 0;            return 1; } Legal ops: ! ~ & ^ | + << >> Max ops: 12 I have a few questions similar to this one, but I'm running into much the same problem for all of them. The behavior of this code appears to be that...
Run the below code and shift binary left and right and understand the output. #include <stdio.h>...
Run the below code and shift binary left and right and understand the output. #include <stdio.h> int main() {     int num=212, i;     for (i=0; i<=2; i++)         printf("Right shift by %d: %d\n", i, num>>i);      printf("\n");      for (i=0; i<=2; i++)         printf("Left shift by %d: %d\n", i, num<<i);             return 0; } Output:
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion *...
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion * I'm guessing we need to modify it Here's the code unmodified public class Sum { //Non recursive sum public static long sum1 (int n) { long sum = 1L; for (int i = 2; i <= n; ++i) sum = sum + i *i ; return sum; } //Recursive sum public static long sum2 (int n) { if (n < 2)return 1L; return sum2(n...
In C can you change this code to use Tail Call Recursion int min (int n,...
In C can you change this code to use Tail Call Recursion int min (int n, int[] input) { int i; int result; for (result = input[0], i = 0; i < n; i += 1) { if (result > input[i]) result = input[i]; } return result; }
Write an R code that does the following: (a) Generate n samples x from a random...
Write an R code that does the following: (a) Generate n samples x from a random variable X that has a uniform density on [0,3]. Now generate samples of Y using the equation: y = α/(x + β). For starters, set α = 1, β = 1 The R code: x=runif(n=1000, min = 0, max = 3) x y=1/x+1 y Please answer the following: b) Use plot(x,y) to check if you got the right curve. c) How does the correlation...
#include <iostream> #include <string> using namespace std; string STUDENT = "ckim84"; // Add your Blackboard login...
#include <iostream> #include <string> using namespace std; string STUDENT = "ckim84"; // Add your Blackboard login name extern string ASSIGNMENT; /** * Describe the purpose of your program here. * @return 0 for success. */ int run() { repeatChar(3, "abcde$"); return 0; } string repeatChar(int n, const string& pat) { string result; for (int i = 0; i < n; i++) { result += pat; } return result; } could you correct my code?? i got an error msg like...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
I have the following RStudio code which works match_prob <- function(x) choose(2*N-x,N)*2 ̂ {-(2*N-x)} It corresponds...
I have the following RStudio code which works match_prob <- function(x) choose(2*N-x,N)*2 ̂ {-(2*N-x)} It corresponds to P(E) = (2N-k,N)(1/2)^(2N-k) I however want to use a slightly different function. Is it possible to input for the following: P(E) = 2(2N-k,N)(1/2)^(2N-k) I tried entering it in by changing the function slightly to: match_prob <- function(x) choose(2*(2*N-x,N))*2 ̂ {-(2*N-x)} and immediately got the error, "Error: unexpected ',' in "match_prob"... The change was a factor of 2 since the container which are considered...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...