Converting to Binary using bitwise and bitshift

北城以北 提交于 2019-12-08 06:09:05

问题


I am trying to create a function to print a number in binary using bitwise and bit shifting but I am having trouble printing it correctly. The following is my code.

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;
}

The following is the output:

0000000000000000000000001280000000

Basically, its printing the 2^bit rather than just a 1 at each bit position (for example if I wanted to convert 7 to binary it would be 0000000...00421 instead of 0000000...00111). This is probably something trivial I'm missing, but any help guys? I've been at this for the last 20 min at can't figure out something so simple.


回答1:


Change decNum & (highestOne >> i) to (decNum & (highestOne >> i)) != 0.

Many people also like to write !!(decNum & (highestOne >> i)). I concede it's cute, but it's less readable and I would suggest that you don't use this.




回答2:


void PrintInBinary( unsigned int decNum )
{

    unsigned int bit;

    for( bit = 1u << (CHAR_BIT*sizeof bit -1); bit; bit >>= 1 ) {
         printf( "%c", decNum & bit ? '1' : '0' );
    }
    printf("\n");
}



回答3:


Use

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



回答4:


That is certainly one way of doing it with the change suggested by Mark, but I think this way is much more readable:

unsigned int decNum = 7;

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



回答5:


If you would prefer to save a arr, i would recommend the next function:

lets look at the next function that get a unsigned int decNum and convert it to binary:

/*#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");



回答6:


decNum & (highestOne >> i) just does the evaluation. If the evaluation is true then you should print 1 else if it is false then print 0.

decNum & (highestOne >> i) ? 1 : 0

NOTE: OTOH, please avoid using magic numbers like 8




回答7:


#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();
}


来源:https://stackoverflow.com/questions/10321928/converting-to-binary-using-bitwise-and-bitshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!