Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't want the solution, just want to understand what this question really asking for?

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.


An example would really help in understanding. Thanks in advance.

What I have tried:

Didn't tried anything, but have to fill for 30 character condition :P
Posted
Updated 4-Jul-17 22:24pm
v2

1 solution

OK: x is (presumably) an integer, which means it is a collection of (say) 32 bits which make up the number: the rightmost bit is the least significant, and the left most is the most significant, and they are numbered from (normally) bit 0 to bit 31, where the index indicates the power of two that the bit holds. If you pretent taht an integer has four bits (to make it simpler to type out):
Bit 0 == 2<sup>0</sup> == zero or one
Bit 1 == 2<sup>1</sup> == zero or two
Bit 2 == 2<sup>2</sup> == zero or four
Bit 3 == 2<sup>3</sup> == zero or eight.

And you can make up the entire range of values that a (four bit) number can hold from those by adding the powers of two together:
Value  B3 B2 B1 B0
  0     0  0  0  0
  1     0  0  0  1
  2     0  0  1  0
  3     0  0  1  1
  4     0  1  0  0
  5     0  1  0  1
  6     0  1  1  0
  7     0  1  1  1
  8     1  0  0  0
  9     1  0  0  1
 10     1  0  1  0
 11     1  0  1  1
 12     1  1  0  0
 13     1  1  0  1
 14     1  1  1  0
 15     1  1  1  1
32 bit numbers work the same way, but with a HUGE range of values!
What the question is asking you to do is to accept an integer value containing existing bits, use an integer which tells you which bit index to start at, a number of bits to play with and a second value to get "new" bits from.

So if n is 6, you want the rightmost 6 bits of y: b31, b30, b29, b28, b27, b26
and if p is 2, you want to remove the six bits of x starting with b2: b8, b7, b6, b5, b4, b3, b2 and replace them with the bits you removed from y.

It sounds complex, but it isn't, not really. Look at the AND operator "&", the OR operator "|", and the shift operators >> and << - they are all you should need for this, pretty much.
 
Share this answer
 
Comments
CPallini 5-Jul-17 5:36am    
5.
the_beginner 6-Jul-17 0:48am    
This is what i came up with, any improvement which can be done to it:
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return((x & ~((~(~0<<n))<<(p-1)))| ((y & ~(~0<<n))<<(p-1)));
}
OriginalGriff 6-Jul-17 1:28am    
And do you find that readable?
Or easy to understand?
the_beginner 6-Jul-17 2:00am    
ok Sorry, let me break it down:
1. Take n righmost bits of Y and place them at location P.
a) ~0<<n --- > to make all bits 1 except rightmost n bits
b) ~(~0<<n) --> get it reverse
c) y & ~(~0<<n) ---> Make all bits except rightmost n bits to 0
d) (y & ~(~0<<n)) << (p-1) --> Move n rightmost bits of Y to position P

2. Now to make n bits of x from position P to 0
a. Same steps a-b from 1
b. ~((~(~0<<n)) << (p-1)) ---> Make n bits from position P as 0 and rest will be 1.
c. x & ~((~(~0<<n)) << (p-1)) --> Make n bits from position P as 0 in x.
3. Finally 'or' between final results from step 1 and 2. to get desired result.
(x & ~((~(~0<<n)) << (p-1))) | ((y & ~(~0<<n)) << (p-1))
OriginalGriff 6-Jul-17 2:25am    
That's not what I meant: if you have to use ten times as much text to explain a single line of code (and you do: 640 characters explanation vs 54 characters code) then your code is impenetrable, and should probably be refactored in order to make it maintainable.
Don't worry about code density: compilers are better optimizers that most developers, and clarity and understandability are far more important than brevity! :laugh:
Yes, you *could* include those human readable instructions as comments - but self documenting code is a better solution because it has a much higher chance of being right, comments are frequently not updated when code is, and that makes them untrustworthy.
Instead, do it in stages: extract and "normalise" the bits. Then remove the unwanted data. Finally combine the two to produce an output. Using human readable variable names for all intermediate stages.

Yes, it's more typing, but it saves you a huge amount of work later when you have to make a change - and one of the unwritten rules of computing is "nothing stays the same"; changes happen all the time. So writing code that is difficult to work with is making your own life harder next week, or next month!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900