some crazy things are happening here...i tried to post my solution two times...both the time the code that got published was completely different from what i wrote. I had to delete the solution therefore...its happening again and again...its not letting me to post the solution...there is some serious bug.
You are given n real numbers in an array. A number in the array is called a decimal dominant if it occurs more than n/10 times in the array. Give an O(n) time algorithm to determine if the given array has a decimal dominant.
Implement a function getbits, that returns the(right adjusted) n bits that begin at position p of an integer. Assume bit position 0 is at the right end and that n and p are sensible positive values.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletesome crazy things are happening here...i tried to post my solution two times...both the time the code that got published was completely different from what i wrote.
ReplyDeleteI had to delete the solution therefore...its happening again and again...its not letting me to post the solution...there is some serious bug.
int a=0,b=31;
ReplyDeletewhile(a<b){
if(((n<<a)&1)^((n<<b)&1)){
n^=1<<a;
n^=1<<b;
}
a++;
b--;
}
hmmmmm...finally...the above code is for any 32 bit integer n.
ReplyDeleteI am unsure if i hv to transform 00...010111 to 00....011101 or 111010...00. The above code is for the latter case.
sorry the inner while loop condition is
ReplyDelete((n>>a)&1)^((n>>b)&1))
@jainendra i think you have posted in some different post..kindly check..:)
ReplyDeletethis code is for reversing individual bits of a number.you have to reverse entire number.
ReplyDeleteeg: 00000101 -> 10100000
you can check this code
ReplyDeleteFor 32-bit integers:
x = ((x >> 16) & 0X0000FFFF) | ((x & 0X0000FFFF) << 16);
x = ((x >> 8) & 0X00FF00FF) | ((x & 0X00FF00FF) << 8);
x = ((x >> 4) & 0X0F0F0F0F) | ((x & 0X0F0F0F0F) << 4);
x = ((x >> 2) & 0X33333333) | ((x & 0X33333333) << 2);
x = ((x >> 1) & 0X55555555) | ((x & 0X55555555) << 1);
x is now the binary reversal of its original value.