C Programming Language
C Programming Language
2nd Edition
ISBN: 9780131103627
Author: Brian W. Kernighan, Dennis M. Ritchie, Dennis Ritchie
Publisher: Prentice Hall
Question
Book Icon
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;
}

Program Plan-

  • 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.

Blurred answer
Students have asked these similar questions
Write a function invert(x,p,n) that returns x with the n bits that begin atposition p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
Q6. Write a function that takes an unsigned integer andreturns the number of '1' bits it has(also known as the Hamming weight).For example, the 32-bit integer '11' has binaryrepresentation 00000000000000000000000000001011,so the function should return 3.T(n)- O(k)   : k is the number of 1s present in binary representation.NOTE: this complexity is better than O(log n).e.g. for n = 00010100000000000000000000000000only 2 iterations are required.Number of loops isequal to the number of 1s in the binary representation."""def count_ones_recur(n): Do it.
Write the function int count101(int n) that counts the number of occurrences of the bit pattern 101 in n. It should return the final count. Variable n is 32 bits. Example: Suppose n is just 8 bits, say n=11010101, then the function would return 3.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr