2012-04-25 29 views
1

我想創建一個函數來打印二進制使用按位和位移的數字,但我無法正確打印它。以下是我的代碼。使用按位和位移轉換爲二進制

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

以下是輸出:

0000000000000000000000001280000000 

基本上,其打印2 ^比特,而不是僅僅一個1在每個比特位置(例如,如果我想轉換7爲二進制它會是0000000 ... 00421而不是0000000 ... 00111)。這可能是我錯過的小事,但任何幫助傢伙?在過去的20分鐘裏,我一直在這個問題上找不到如此簡單的東西。

+2

順便說一句,而不是'的sizeof(int)的* 8',您可能需要使用'的sizeof (int)* CHAR_BIT'。 – 2012-04-25 18:51:56

+0

使用像8這樣的神奇數字並不好。 – 2012-04-25 18:55:40

回答

3

decNum & (highestOne >> i)更改爲(decNum & (highestOne >> i)) != 0。很多人也喜歡寫!!(decNum & (highestOne >> i))。我承認它很可愛,但它的可讀性較差,我建議你不要使用它。

+0

嗯,它的工作!但是我的實現出了什麼問題?我以爲使用&操作符導致1或0? – 2012-04-25 18:52:52

+1

@ user1343030不,在某些時候,你有'128&128',它是128.你得到一個數字,這些數字在這兩個操作數中都被重新設置。 – 2012-04-25 18:54:55

+0

對,我忘記了右邊的0也被算作數字的一部分。謝謝! – 2012-04-25 18:58:58

1

使用

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

decNum & (highestOne >> i)只是做了評價。如果評估是true那麼您應該打印1否則如果它是false然後打印0

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

注:OTOH,請避免使用幻數像8

1

這當然是由Mark建議的變化做的一種方式,但我認爲這種方式更可讀:

unsigned int decNum = 7; 

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

嗯,這也是一個創造性的方式來做到這一點! – 2012-04-25 19:10:12

1

如果你希望保存改編,我會建議下一個功能:

讓我們來看看這得到一個unsigned 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"); 
0
#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(); 
} 
相關問題