Question

In: Computer Science

Please make a C program named: rotate.c This code will contain a single function (and the...

Please make a C program named:

rotate.c

This code will contain a single function (and the include directive to your header file) to perform the rotation operation using the two integer operands passed to this function. The first integer operand is the number to be rotated. The second integer operand is the number of bit positions that the first operand must be rotated by.

EXAMPLE:

Enter a 32-bit number (>= 1 and <= 4294967295, inclusively): 1

Enter the number of positions to rotate-right the input (between 0 and 31, inclusively): 1

1 rotated by 1 position gives: 2147483648

Solutions

Expert Solution

The following is the code for above question.

#include <stdio.h>
#define BITS 32
int rotate(int number,unsigned int bit_positions)
{ //here we know, in number>>bit_positions the first bit_positions bits are 0. BITS-bit_positions gives the last digits and hence bitwise between number>>bit_positions) and number<<(BITS-bit_positions) will rotate the number right by bit_positions digits 
    return (number>>bit_positions)|(number<<(BITS-bit_positions));
}
int main()
{
    int number,bit_positions;
    printf("Enter a 32-bit number (>= 1 and <= 4294967295, inclusively): ");
    scanf("%d",&number);
    printf("\nEnter the number of positions to rotate-right the input (between 0 and 31, inclusively): ");
    scanf("%d",&bit_positions);
    printf("\n%d rotated by %d position gives: %d",number,bit_positions,rotate(number,bit_positions));
    return 0;
}

I am also attaching the output for your reference.

Output:

Enter a 32-bit number (>= 1 and <= 4294967295, inclusively): 1

Enter the number of positions to rotate-right the input (between 0 and 31, inclusively): 1

1 rotated by 1 position gives: -2147483648

#Please dont forget to upvote if you find the solution helpful. Thank you


Related Solutions

C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
C Programming Please write a single function and not the whole program Can you please provide...
C Programming Please write a single function and not the whole program Can you please provide comments and explanations for all questions. Thank you Write a single function for the following: void split_date(int day_of_year, int year, int *month, int *day);             day_of_year is an integer between 1 and 366, specifying a particular day within the year designated by the parameter year. Month and day point to variables in which the function will store the equivalent month (1 – 12) and day...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt). It should also print the values of the array all on the same line, separated by spaces. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array. Write...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
in c++, please provide only a function. the function should be named "tally." it takes no...
in c++, please provide only a function. the function should be named "tally." it takes no parameters but returns an int. the first int should be 0, next being 1, then 2, etc.. will rate if correct. again should only be 1 function.
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java...
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java that incorporates the following: An abstract Bicycle class that contains private data relevant to all types of bicycles (cadence, speed, and gear) in addition to one new static variable: bicycleCount. The private data must be made visible via public getter and setter methods; the static variable must be set/manipulated in the Bicycle constructor and made visible via a public getter method. Two concrete classes...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Make a python code. Write a function named max that accepts two integer values as arguments...
Make a python code. Write a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Write the program as a loop that...
Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT