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
complete the following c puzzles using the listed operators. no data control structures allowed (for, if,...
complete the following c puzzles using the listed operators. no data control structures allowed (for, if, etc.) /* * logicalShift - shift x to the right by n, using a logic\ al shift * Can assume that 0 <= n <= 31 * Examples: logicalShift(0x87654321,4) = 0x08765432 * Legal ops: ! ~ & ^ | + << >> * Max ops: 20 * Rating: 3 */ int logicalShift(int x, int n) { } /* * bitCount - returns count of...
You can't use any constant larger than 8 bits Please don't use codes from other websites...
You can't use any constant larger than 8 bits Please don't use codes from other websites C program Task: * rotateLeft - Rotate x to the left by n bits * Can assume that 0 <= n <= 31 * Examples: rotateLeft(0x87654321,4) = 0x76543218 * Legal ops: ~ & ^ | + << >> ! * Max ops: 25 * Rating: 3 */ int rotateLeft(int x, int n) { return 2; }
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; }
For different input n, i.e., n = 1, n = 10, n = 20, write down...
For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation. #include <iostream> using namespace std; void function1(int n){ int count = 0; for(int x = 0; x < 12; x++){ cout<<"counter:"<< count++<<endl; } } void function2(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x="<<x<<"------"<<endl; for(int i =0; i < n;...
I'm looking for an in-depth explanation on how each of these bitwise functions work. I think...
I'm looking for an in-depth explanation on how each of these bitwise functions work. I think I understand the underlying concepts of each, but I want to be sure. I'm looking for a piece by piece break down of each function. Function 1 int logicalShift(int x, int n) { x >>= n; return x & ~(~(~0 << n) << (32 + ~n + 1)); Function 2 { int bang(int x) { int neg = ~x + 1; return ((~(x |...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...
Can you translate this C code into MIPS assembly with comment? #include <stdio.h> #include <math.h> #include...
Can you translate this C code into MIPS assembly with comment? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT