C 使用按位和位移位转换为二进制

C 使用按位和位移位转换为二进制,c,bit-manipulation,bit-shift,C,Bit Manipulation,Bit Shift,我正在尝试创建一个函数,使用按位和位移位以二进制形式打印数字,但我无法正确打印它。下面是我的代码 void PrintInBinary( unsigned int decNum ) { int i = 0; unsigned int highestOne = 1 << (sizeof(unsigned int)*8 - 1); for( i = 0; i < sizeof(int)*8; i++ ) { printf( "%u", d

我正在尝试创建一个函数,使用按位和位移位以二进制形式打印数字,但我无法正确打印它。下面是我的代码

void PrintInBinary( unsigned int decNum )
{
    int i = 0;
    unsigned int highestOne = 1 << (sizeof(unsigned int)*8 - 1);

    for( i = 0; i < sizeof(int)*8; i++ ) {
         printf( "%u", decNum & (highestOne >> i) );
    }
    printf("\n");
}


int main()
{
    unsigned int a = 128;
    PrintInBinary( a );
    system("PAUSE");
    return 0;
}
基本上,它是在每个位位置打印2^位而不是1,例如,如果我想将7转换为二进制,它将是0000000…00421,而不是0000000…00111。这可能是我遗漏的一些琐碎的东西,但是有人帮我吗?在过去的20分钟里,我一直在做这个,我想不出这么简单的事情。

将decNum&highestOne>>I更改为decNum&highestOne>>I!=0

很多人也喜欢写作!!decNum和highestOne>>i。我承认它很可爱,但可读性较差,我建议您不要使用它。

将decNum&highestOne>>I更改为decNum&highestOne>>I!=0

很多人也喜欢写作!!decNum和highestOne>>i。我承认它很可爱,但可读性较差,我建议您不要使用它。

使用

printf( "%u", decNum & (highestOne >> i) > 0 ? 1 : 0 );
使用

decNum&highestOne>>我只是做评估。如果评估为真,则应打印1;如果评估为假,则打印0

注意:OTOH,请避免使用像8这样的幻数,我只是做评估。如果评估为真,则应打印1;如果评估为假,则打印0


注意:OTOH,请避免使用像8这样的神奇数字,这当然是Mark建议的改变的一种方法,但我认为这种方法更具可读性:

unsigned int decNum = 7;

for(i = 0; i < sizeof(int)*8; i++ ) 
{
  printf("%u", ((decNum >> i) & 1));
}
printf("\n");

这当然是马克建议的改变的一种方式,但我认为这种方式更具可读性:

unsigned int decNum = 7;

for(i = 0; i < sizeof(int)*8; i++ ) 
{
  printf("%u", ((decNum >> i) & 1));
}
printf("\n");

如果您希望保存arr,我建议使用下一个函数:

让我们看看下一个函数,它获取无符号int decNum并将其转换为二进制:

/*#define BITS 8*/
int size = sizeof(unsigned int)*BITS;

char arr[size] ;

int i;
/* 
    now lets thinkk...

    shift by i=0 to the right:
    4 = 00...00 0100 &
    1 = 00...00 0001
    -----------------
        00...00 0000
    now we know that we need to enter 0 in the 1-rd place in the arr

    shift by i=1 to the right:
    4 = 00...00 0010 &
    1 = 00...00 0001
    -----------------
        00...00 0000
    now we know that we need to enter 0 in the 2-rd place in the arr

    shift by i=2 to the right:
    4 = 00...00 0001 &
    1 = 00...00 0001
    -----------------
        00...00 0001
    now we know that we need to enter 1 in the 3-rd place in the arr

    and so on...

 */
    for(i=0; i<size; ++i) {
         int shifted = (decNum >> i);
         arr[(size-1)-i] = (shifted&1)?'1':'0';
    }

printf("The binary of %d in %d bits:\n",decNum, size);

/*now lets print the array*/
for (i=0; i < size ; i++){
         printf("%c",arr[i]);
}
printf("\n");

如果您希望保存arr,我建议使用下一个函数:

让我们看看下一个函数,它获取无符号int decNum并将其转换为二进制:

/*#define BITS 8*/
int size = sizeof(unsigned int)*BITS;

char arr[size] ;

int i;
/* 
    now lets thinkk...

    shift by i=0 to the right:
    4 = 00...00 0100 &
    1 = 00...00 0001
    -----------------
        00...00 0000
    now we know that we need to enter 0 in the 1-rd place in the arr

    shift by i=1 to the right:
    4 = 00...00 0010 &
    1 = 00...00 0001
    -----------------
        00...00 0000
    now we know that we need to enter 0 in the 2-rd place in the arr

    shift by i=2 to the right:
    4 = 00...00 0001 &
    1 = 00...00 0001
    -----------------
        00...00 0001
    now we know that we need to enter 1 in the 3-rd place in the arr

    and so on...

 */
    for(i=0; i<size; ++i) {
         int shifted = (decNum >> i);
         arr[(size-1)-i] = (shifted&1)?'1':'0';
    }

printf("The binary of %d in %d bits:\n",decNum, size);

/*now lets print the array*/
for (i=0; i < size ; i++){
         printf("%c",arr[i]);
}
printf("\n");

作为旁白,代替sizeofint*8,您可能想使用sizeofint*CHAR\u位。使用像8这样的幻数是不好的。作为旁白,代替sizeofint*8,您可能想使用sizeofint*CHAR\u位。使用像8这样的幻数是不好的。嗯,它起作用了!但我的实现有什么问题?我认为使用&运算符会得到1或0?@user1343030不,在某个点上,你有128&128,也就是128。你得到了一个在两个操作数中都重新设置了位的数字。对了,我忘记了位右边的0也被算作数字的一部分。谢谢@user1343030:我忘了位右边的0也算作数字的一部分。为了帮助你,你应该给我开一张1000000美元的支票。只有一美元-嗯,成功了!但我的实现有什么问题?我认为使用&运算符会得到1或0?@user1343030不,在某个点上,你有128&128,也就是128。你得到了一个在两个操作数中都重新设置了位的数字。对了,我忘记了位右边的0也被算作数字的一部分。谢谢@user1343030:我忘了位右边的0也算作数字的一部分。为了帮助你,你应该给我开一张1000000美元的支票。只有一美元-嗯,这也是一种创造性的方式!嗯,这也是一种创造性的方式!
/*#define BITS 8*/
int size = sizeof(unsigned int)*BITS;

char arr[size] ;

int i;
/* 
    now lets thinkk...

    shift by i=0 to the right:
    4 = 00...00 0100 &
    1 = 00...00 0001
    -----------------
        00...00 0000
    now we know that we need to enter 0 in the 1-rd place in the arr

    shift by i=1 to the right:
    4 = 00...00 0010 &
    1 = 00...00 0001
    -----------------
        00...00 0000
    now we know that we need to enter 0 in the 2-rd place in the arr

    shift by i=2 to the right:
    4 = 00...00 0001 &
    1 = 00...00 0001
    -----------------
        00...00 0001
    now we know that we need to enter 1 in the 3-rd place in the arr

    and so on...

 */
    for(i=0; i<size; ++i) {
         int shifted = (decNum >> i);
         arr[(size-1)-i] = (shifted&1)?'1':'0';
    }

printf("The binary of %d in %d bits:\n",decNum, size);

/*now lets print the array*/
for (i=0; i < size ; i++){
         printf("%c",arr[i]);
}
printf("\n");
#include"stdio.h"
#include"conio.h"//this coding f

void main()
{
    int rm,vivek;
    clrscr();
    printf("enter the values");
    scanf("%d",&rm);
    printf("enter the no.of times moves");
    scanf("%d",&vivek);
    printf("the value rm=%d>>vivek=%doutput=%u",rm,vivek,rm>>vivek);//5>>1
    getch();
}