The binary conversion problem in the interview question, will be a binary number, starting from a bit, n-bit conversion, the program is as follows:
1 /************************************************************************/2 /*function: Realize the conversion of binary number x, p is the number of conversion bits, n is the conversion length. Example x=0b0001 0001,p=4,n=3 after conversion x=0b0110 00013 / * ZL4 /* Date: 2018-04-08 14:385 /************************************************************************/6 7#include <stdio.h>8 9UnsignedintConvert (unsignedintXintPintn);Ten voidD_to_b (unsignedintx); One A - intMainvoid) - { the -Unsignedintx= -; -Unsignedinty; - +printf"before conversion:"); - D_to_b (x); +printf"\ n"); A atY=convert (x,4,3); -printf"after conversion:"); - D_to_b (y); -printf"\ n"); - - in - } to + -UnsignedintConvert (unsignedintXintPintN) the { *Unsignedintbit=0; $Unsignedinttemp=1;Panax Notoginseng intI=0; - the for(i=0; i<n;i++) + { ABit |=temp; thetemp<<=1; + } - $bit=bit<<p; $X ^=Bit; - returnx; - } the - voidD_to_b (unsignedintx)Wuyi { the intI=0, j=0; - if(x==0) Wu { - return; About } $ Else - { -i=x%2; -j=x/2; A D_to_b (j); +printf"%d", i); the } -}
The program runs in vc++6.0 as shown in the results:
C Language Learning (iv)