@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.
Given an integer n, write a function that returns count of trailing zeroes in n!. Examples: Input: n = 5 Output: 1 Factorial of 5 is 20 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24
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.