Click here to Skip to main content
15,906,645 members

Comments by the_beginner (Top 42 by date)

the_beginner 6-Jul-17 2:41am View    
thanks for reply, will keep in mind
the_beginner 6-Jul-17 2:00am View    
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))
the_beginner 6-Jul-17 0:48am View    
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)));
}
the_beginner 4-Jul-17 8:23am View    
Thanks for reply. So when I am entering "abaad", after hitting Enter, c is taking values one by one from "abaad" and then going through while loop ?
the_beginner 21-Jun-17 4:26am View    
thanks, got it