@priyaranjan: I always encounter problems in bitwise operator.can u please give a link from where i can understand these... also is it neccessary that this question can be done only using bitwise operator..
@kamakshi no it is not mandatory to do it using bitwise but as you must be knowing there is no intrinsic support of BOOL value in c,so we have to operate on individual bits. In your code you are taking an integer array,so it is not efficient. Try to solve many problems related to bitwise.
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.
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.
assuming the number is of 4 bits only..
ReplyDeleteint oddbitsmask=0x0101;
int evenbitsmsk=0x1010;
int ans=((number & oddbitmask)<<1) &((number&evenbitmask)<<1);
@ankit continuing with same logic for a number with 8 bits
ReplyDeletemask will be
int oddbitsmask=0xAA;
int evenbitsmsk=0x55
it should be like this:-
ReplyDeleteans=( ( n & oddbitmask ) << 1 ) | ( ( n & evenbitmask ) >> 1 );
@ankit please correct your answer...
ReplyDelete@priyaranjan:
ReplyDeleteI always encounter problems in bitwise operator.can u please give a link from where i can understand these...
also is it neccessary that this question can be done only using bitwise operator..
according to me,
int swap(int a[])
{
for(i=0;i<=3;i++)
{
swap(a[2*i],a[(2*i)+1]);
}
return a;
}
@sorry...
ReplyDeletethe above code be lyk dis..
int interchange(int a[])
{
for(i=0;i<=3;i++)
{
swap(a[2*i],a[(2*i)+1]);
}
return a;
}
@kamakshi no it is not mandatory to do it using bitwise but as you must be knowing there is no intrinsic support of BOOL value in c,so we have to operate on individual bits.
ReplyDeleteIn your code you are taking an integer array,so it is not efficient.
Try to solve many problems related to bitwise.