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 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.
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 function that keeps computing the sum of the digits of x^n, until there is only one digit left. For example, sumOfDigits(2,5) returns 5, because 2 ^ 5 = 32 = 3 + 2 = 5 Another example, sumOfDigits(2,8) returns 4, because 2 ^ 8 = 128 = 1 + 2 + 8 = 13 = 1 + 3 = 4arrow_forwardThe Mux below implements the function F = 8 to 1 MUX ABC F Select one: O a. F = A'B'C + AB'C + ABC O b. F = A'B'C' + AB'C + AB'C + ABC' OC F=A'B'C' + A'BC' + A'BC + AB'C' + AB'C' O d. F= A'B'C + A'BC' + A'BC + ABCarrow_forwardHAHAAAAAAHN HNM1000 OHNM 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27 28 30 31 32 section . data .section .text .globl _start start: pushq $4 pushq $3 call function-A addq $16, $rsp movg %rax, %rbx movq $1, rax int $0x80 .type function-A, @function function-A: pushq rbp movq rsp, rbp. subq $8, rsp mova 16 (%rbp), rbx movg 24 (rbp), rcx movg %rbx, -8(%rbp) label-A: #variable y #variable z cmpq $1, $rcx je label-B movq -8 (&rbp), rax imulg %rbx, srax movq rax, -8 (%rbp) decq rcx jmp label-A label-B: movq -8 (Srbp), rax movq rbp, rsp popq &rbp retarrow_forward
- Q11/ A function f(t) is said to be even if: Oa) f(t) = f(-t) Ob) f(t) = -f(-t) Oc) f(t) # f(-t) %3D Od) None of thesearrow_forwardIn Pyhton IDLE Write a function that takes the first 10 even numbers and adds them to the first 10 odd numbers. They must be added as pairs. That is, the first even number with the first odd number (such as 1 + 2, 3 + 4, etc.). The add each of these pairs. Return the sum of all of the two number pairs as a single sum.arrow_forwardWrite a function that takes the number n, which reverses the binary representation of that number, and then returns the new number from the returned binary. Examples BinaryInteger (10) - 5 // 10 = 1010 -> 0101 = 5 BinaryInteger (12) → 3 // 12 = 1100 -> 0011 = 3 BinaryInteger (25) → 19 // 25 = 11001 -> 10011 = 19 BinaryInteger (45) → 45 // 45 = 101101 -> 101101 = 45arrow_forward
- Write a function that prints the following pattern of n lines. * *+* * * * * + **+*+*+*+*arrow_forwardWrite a function setbits(x,p,n,y) that returns x with the n bits that begin atposition p set to the rightmost n bits of y, leaving the other bits unchanged.arrow_forwardWrite a function that prints the ASCII values of the char- acters using the following header: void printASCII(char ch1, char ch2, int numberPerLine) This function prints the ASCII values of characters between ch1 and ch2 with the specified number of characters per line. Write a test program that prints 6 ASCII values per line of characters from 'a' to 'm'.arrow_forward
- Defining a binary number as Program 1, write the function int binToDec(const int bin[]) to convert an eight-bit unsigned binary number to a nonnegative decimal integer. Do not output the decimal integer in the function. Test your function with interactive input. Defining bAnd, bin1, and bin2 as binary numbers as in Program 1 above, write the void function void binaryAnd(int bAnd[], const int bin1[], const int bin2[]) to compute bAnd as the logical AND of the two binary numbers bin1 and bin2. Do not output the binary number in the function. Test your function with interactive input. Program1 in C: #include <stdio.h>int main(){int binNum[8]; // Array to read binary numberlong dec=0,n=0; // variables used to convert binary to decimalint k=0,l=0;long binary=0;int i=1,j=0,remainder=0; //reading the binary number in to the array binNumprintf("Please Enter the first binary number with each bit seperate by at least one space : \n"); scanf("%d %d %d %d %d %d %d…arrow_forwardWrite 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.arrow_forwardWrite a function timestable(n), which prints a multiplication table of size n. For example, timestable(5) would print: 1 2 3 4 5 2 3 4 5 4 6 8 10 6 9 12 158 12 16 2010 15 20 25Use the function in a program where you ask the user for n and you print the corresponding table.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr