4 ms·
Well, the problem is: Write a program setbits(x, p, n, y); which returns x with the n bits that begin at position p set to the rightmost n bits in y, leaving th
by t3rr0r1z3r 17y ago
Well, the problem is: Write a program setbits(x, p, n, y); which returns x with the n bits that begin at position p set to the rightmost n bits in y, leaving the other bits unchanged.
I've tried to solve it.
To answer your questions; Yes, I understand binary representation,
11 is 1011
12 is 1100
25 is 11001
-9 is 1001010 (where the leftmost bit is set to 1)
- RiderOfGiraffes 17y agoWell, -9 isn't 1001010, but we can explore that another time. The real help would be if you showed what you've tried. However: How can you extract the rightmost (least significant) 3 bits of a number x? How can you move the bits in a number to the left (make them more significant)? How can you turn off some bits in a number? Can you puzzle out how these questions/answers can be glued together to do what you want? Have you tried doing it by hand on paper? Here: x = 1010101010101 p = 4 n = 5 y = 11111111 Show us what you actually need to do, step by step. PS: This probably isn't the forum to have a tutorial on such questions, and I'm going off line in a few minutes. If no one else answers your questions, email me. PPS: Where does this question come from? What resources do you have?
- deleted 17y ago[deleted]