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 7E
Program Plan Intro
Program Plan-
- Initialize the header files and main() function.
- To define the function invert(x, p, n, y) to return x with the n bits that begin at position p inverted( changing 0 to 1 and 1 to 0), leaving the other bits unchanged.
- Finally prints the result.
Summary Introduction- The program displays the x value after inverting some bits using bitwise operators.
Program Description- The purpose of the program is todefine the function invert(x, p, n, y) to return x with the n bits that begin at position p inverted( changing 0 to 1 and 1 to 0), leaving the other bits unchanged.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
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.
If 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 & I5
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.
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
- Q2. Write a forward pass function forward(x, w, b, func) to calculate variables z, ŷ, L, with given x, y, w, b I def forward(x, w, b, func): 2, yhat, L = None, None, None # add your code here return z, yhat, Larrow_forwardwe will pretend that bits are sent as strings (theyare not; this would be terribly inefficient). Write a function evenParity(bits) that returns True if the string bits haseven parity and False otherwise. For example, evenParity('110101')should return True and evenParity('110001') should return Falsearrow_forwardWrite a function rightrot(x,n) that returns the value of the integer x rotatedto the right by n positions.arrow_forward
- (a) Convert n to binary form, call it B. You may use the bin( ) function. (b) Split B into two “halves”, call them F and S, where if the length of B is odd, then include the extra digit in the first half. For example, if B = 101110, then the first half is F = 101 and the second half is S = 110. As another example, if B = 1100111, then the first half isF = 1100 and the second half is S = 111. (c) Convert F and S to decimal form, add one to each decimal form, and then return the product of these new values.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 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_forward
- What is the function of the following program (attached photo) LXI H 5000h MVI A 00h MOV B M INX H MOV C M AG: ADD B DCR C JZ STOR JMP AG STOR: INX H ADD M STA 5003h HLT O summation of two numbers (X, Y) and store result in M(5000h) OY = Factorial of (X) and store result in M(5000h) OX power to Y and store result in M(5000h) O Nonearrow_forwardWrite a function int Factorial(int n) to calculate factorial of n. If n is non negative then it return factorial, otherwise it display a proper error message and return -1.arrow_forwardWrite a function that prints the following pattern of n lines. * *+* * * * * + **+*+*+*+*arrow_forward
- In loop functions.py, define a function named permutations that takes two paramters, n and r, in that order. This function returns the number of different permutations of length r from set of size n. Essentially to calculate these permutations, the function should compute the product (n - r+1)*(n-r+2)*(n-r+3)*...*(n-2)*(n-1) or n!/(n-r)!. No if statements or built in math functions should be used. For or while loops are best reccomended.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_forwardIn Python write a function decoding() that takes an encoded string and an integer as the parameters, and returns the original string. The encoding was performed as follows. First, the original string is reversed. Then, each character in the new string is replaced by the character that is a given number of positions away in the ASCII character encoding table. For example, the following call to yur function : encoding('ABCabc', -10) should return the encoded string 'YXW987'.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