C Programming Language
2nd Edition
ISBN: 9780131103627
Author: Brian W. Kernighan, Dennis M. Ritchie, Dennis Ritchie
Publisher: Prentice Hall
expand_more
expand_more
format_list_bulleted
Question
Chapter 2, Problem 8E
Program Plan Intro
Method definition of rightrot(x,n):
/*function definition of right rot*/ unsignedint rightrot(unsignedint x,unsignedint n) { /*loop till the bit positions*/ while(n>0) { /*condition to rotate each bit by one position.*/ if(x&1) { x =(x >>1)|~(~0U>>1); } else { x =(x >>1); } n--; } return x; }
- Initialize the header files and main() function.
- To define the function rightrot(x, n) to rotate x right by n bits.
- Finally prints the value of x..
Summary Introduction- The program displays the x value after rotating it right by n bits.
Program Description- The purpose of the program is todefine the function rightrot(x, n) to return x with the n bits rotated towards right.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position
p set to the rightmost n bits of y, leaving the other bits unchanged.
In C, write a function int setbit(int n, int i) to set the i^ᵗʰ bit of n if i^ᵗʰ bit is 0.
Write a function that makes the base conversion. It takes two integers n and b as input (n >= 0 andb > 1, both in base 10) and expresses n in base b. For example, the decimal expansion of 345 inbase 10 is 345 = 3 × 102 + 4 × 101 + 5 × 100.Sample input: n = 12, b =2. Sample output: 1100Explanation: (12)10 = (1100)2
Chapter 2 Solutions
C Programming Language
Ch. 2 - Prob. 1ECh. 2 - Write a loop equivalent to the for loop above...Ch. 2 - Write the function htoi(s), which converts a suing...Ch. 2 - Write an alternate version of squeeze(s1,s2) that...Ch. 2 - Prob. 5ECh. 2 - Prob. 6ECh. 2 - Prob. 7ECh. 2 - Prob. 8ECh. 2 - Prob. 9ECh. 2 - Rewrite the function lower, which converts upper...
Knowledge Booster
Similar questions
- Write a C-function with two arguments (n and r) that has prototype: char clearbit(char k, char bits) The function clears (sets to 0) the bit number k (in the range of 0 to 7) in bits and returns the resulting value. For example, if k is 0x02 and bits is 0x07, the function would return bits with its k’th bit cleared, resulting in 0x03. It must not change other bits in bits. Hint: You may use any number of C-statements, but this task can be accomplished in as few as one!arrow_forwardc language Write a function which takes an integer and returns 1 if there are an odd number of ‘1’ bits 0 if there are an even number of ‘1’ bits Example, of x is 1010 in binary, it should return 0 because there are two 1's. 1110 should return 1 because there are three 1'sarrow_forward// Task 3 // For this function, you must return the largest power of 2 that // is less than or equal to x (which will be positive). You may // not use multiplication or some sort of power function to do this, // and should instead rely on bitwise operations and the underlying // binary representation of x. If x is 0, then you should return 0. unsigned largest_po2_le(unsigned x) { return x; }arrow_forward
- If you implement the function F(A,B,C)= Sum m (2,4,6,7) using a 2-1 MUX then the input position I0 will be: a. None of the choices b. I0 = AB' c. I0 = C' d. I0 = BC' e. I0 = AC f. I0 = 1arrow_forwardIf you implement the function F(A,B,C,D)= Sum m (3,4,6,11,12,13,14) using a 8-1 MUX then which of the input position will be equal to D : a. I1 & I6 b. only I5 c. I2 & I6 & I7 d. Only I7 e. None of the choices f. I1 & I5arrow_forwardWrite a function in C++ that takes two arguments, and implements 2's complement addition. Note: the two arguments are a signed (w-bit) integers. the w is also an argument.arrow_forward
- Suppose we number the bytes in a w-bit word from 0 (least significant) to w/8 – 1 (most significant). Write code for the following C function, which will return an unsigned value in which byte i of argument x has been replaced by byte b: unsigned replace_byte (unsigned x, int i, unsigned char b); Here are some examples showing how the function should work: replace_byte(0x12345678, 2, 0xAB) --> 0x12AB5678 replace_byte(0x12345678, 0, 0xAB) --> 0x123456AB Bit-Level Integer Coding Rules In several of the following problems, we will artificially restrict what programming constructs you can use to help you gain a better understanding of the bit-level, logic, and arithmetic operations of C. In answering these problems, your code must follow these rules: Allowed operations All bit-level and logic operations. Left and right shifts, but only with shift amounts between 0 and w – 1. Addition and subtraction. Equality (==) and inequality (!=) tests. (Some of the problems do not allow…arrow_forwardImplement the following error detection and error correction algorithms using C programming for Hamming code. For Hamming codes, flip a bit in the data and implement the algorithm to correct the same on the receiver side.arrow_forwardDetermine whether f is a function from the set of all bit strings to the set of integers if a) f (S) is the position of a 0 bit in S. b) f (S) is the position of a 1 bit in S.arrow_forward
- Write a C++ program that calculates the division of binary, use the BinDiv( ) - a function that performs binary division Sample Output Enter First Number: 11101 Enter Second Number:101 THE DIVISION OF THE TWO BINARY NUMBERS: 101.1100110011001101arrow_forwardmodify the program in the pics so its inputs a list of 16-bit integers from the user, and then displays the integers in reverse order.arrow_forwardSuppose we call a function that calculates the center of gravity of an aircraftusing the Q15.16 format for the return and the returned real number is 3.25. What will be thevalue in R0 before the function returns in hexadecimal or binary?arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education