Here is my code:
#include
#include
Int main ()
{
Unsigned int x, n.
Int I, y.
Char s [50];
The scanf (" % u ", & amp; X);
i=0;
While (x> 0)
{
S [i++]=x % 2 + '0';
X=x/2;
}
S [I]='\ 0';
Y=I - 1;
For (I=y; I>=0; -) if I ((I + 1) % 2==0) s [I]='0';
For (I=y, n=0; I>=0; I -) n +=pow (2.0 y - I) * (s [I] - '0');
Printf (" % u \ n ", n);
return 0;
}
CodePudding user response:
Need to be so complicated?Use & amp; Operation good? Binary 1010 is the hexadecimal a
So
Y=x & amp; 0 xaaaaaaaa
It is ok
CodePudding user response:
# include & lt; Stdio. H>
#include
Unsigned int get_odd_bit (unsigned int n, int bit_len);
Int main ()
{
# if 0
Unsigned int x, n.
Int I, y.
Char s [50];
The scanf (" % u ", & amp; X);
i=0;
While (x> 0)
{
S [i++]=x % 2 + '0';
X=x/2;
}
S [I]='\ 0';
Puts (s);
Y=I - 1;
For (I=y; I>=0; -) if I ((I + 1) % 2==0) s [I]='0';
Puts (s);
For (I=y, n=0; I>=0; I -) n +=pow (2.0 y - I) * (s [I] - '0');
Printf (" % u \ n ", n);
# the else
Unsigned int x;
The scanf (" % u ", & amp; X);
X=get_odd_bit (x, 32);
Printf (" % u \ n ", x);
# endif
return 0;
}
Unsigned int get_odd_bit (unsigned int n, int bit_len)
{
Unsigned int s=0;
int i;
For (I=0; I & lt; Bit_len; I++) {
If (I % 2)
S +=((n & gt;> I) & amp; 1 u)? (1 u & lt; }
Return s;
}
For your reference
The building code is no problem, but do not conform to the requirements of the subject, because the topic request custom function implementation, the original poster write logic in the main function,
Suggest the building their own logic to try using a custom function, the above is implemented using a custom function, consult