2010-02-03 39 views
1

我想在c中打印出二进制数,但是我遇到的困境是它的打印顺序相反。我定义一个函数来告诉我有多少位有,这样我可以从最后一位下班回来逆转订单的麻烦

获得第N位,我可以使用

(value >> totalNumberOfBits) & 1; 

在while循环,我可以运行这个直到totalNumberOfBits == 0;

这样

while(totalNumberOfBits!= 0){ 
    putchar(n >> totalNumberOfBits)&1; 
    totalNumberOfBits--; 
} 

任何指针将受到欢迎 - 我想我可能是massivley的时候,我有一个打印他们的微细backw的方法ARDS但IAM试图找到避免这种

感谢

+0

为了澄清,你要打印出来的大端或小端? – Aistina 2010-02-03 14:00:35

+0

最重要的位首先,所以我认为这是大endian? – leo 2010-02-03 14:08:22

+0

是的。阅读我的答案:) – Aistina 2010-02-03 14:37:52

回答

0
while (totalNumberOfBits != 0) { 
    putchar(n >> totalNumberOfBits) & 1; 
    totalNumberOfBits--; 
} 

好了,你的代码是很接近(并且实际上已经打印出正确的顺序位),但有3个个小错误。首先,在编译的Visual Studio时,给了我以下警告:

warning C4552: '&' : operator has no effect; expected operator with side-effect 

据抱怨你的代码,你似乎不小心放置在您的putchar函数调用的括号外的& 1一部分。

while (totalNumberOfBits != 0) { 
    putchar((n >> totalNumberOfBits) & 1); 
    totalNumberOfBits--; 
} 

第二个错误是,虽然它现在正确地打印了位,但您正在打印\ 0和\ 1个字符。 \ 0不会显示在控制台中,并且\ 1很可能看起来像是一个笑脸,所以让我们来修复它。

while (totalNumberOfBits != 0) { 
    putchar(((n >> totalNumberOfBits) & 1) ? '1' : '0'); 
    totalNumberOfBits--; 
} 

现在非常接近,只剩下一个小小的错误。由于检查while循环执行的操作,以及您减少totalNumberOfBits的位置,即使您的n只有8位(因此超出范围),您也不检查2^0的位,而检查2^8。所以我们移动减量和替代!=

while (--totalNumberOfBits >= 0) { 
    putchar(((n >> totalNumberOfBits) & 1) ? '1' : '0'); 
} 
+0

谢谢Aistina, 是不是破旧我的原始尝试然后:D – leo 2010-02-03 14:56:29

0

而是右移位的方式,尽量留在WORD尺寸移动和结束。或者使用WORD大小减去位数,然后先删除前导0。

另外不要忘记更改您的&以匹配最高位。

1

您的putchar(n >> totalNumberOfBits)&1是一个错误(注意右侧paren在哪里)。

你目前的代码输出看似随机的值(根据你的号码中'当前'和更重要的位),如果你只是将右边的父母移到你想表达的位置,那么你将会写'\0''\1'(它们是空的和另一个控制代码)。相反,你想写'0''1'(数字零和一)。

这是最好用一个例子所示的一件事情:

void f() { 
    // hardcode total_bits values for this example 
    // in reality you'd call your function 
    int num = 42; // 101010 in binary 
    for (int total_bits = 6; total_bits;) { 
    putchar("01"[(num >> --total_bits) & 1]); 
    } 

    num = 5; // 101 in binary 
    // loop written verbosely, but does exactly the same: 
    for (int total_bits = 3; total_bits;) { 
    --total_bits; // decrement after condition is checked, before used 
    int bit = (num >> total_bits) & 1; // bit is always 0 or 1 
    char c = "01"[bit]; // c is always '0' or '1' 
    putchar(c); 
    } 
} 

记下减量(可如果你想移动,而不是for循环—将改变第三部分计算顺序),并且此循环不处理num为零时的情况。

0
static void 
print_binary(int value, int numBits) 
{ 
    /* postfix decrement, so the loop will run numBits times */ 
    while (0 < numBits--) { 

     /* 
     * Since numBits was decremented it now is an index to the next bit from 
     * the left. So, we shift a one to the left that number of bits, do a 
     * bitwise-AND with the value and test whether it is not equal to 0. If 
     * so, print a 1. Otherwise, print a 0! 
     */ 
     putchar((value & (1 << numBits)) ? '1' : '0'); 
    } 
} 
1

会这样做,通过使用itoa功能转换为数字,并将其存储到缓冲区中,然后使用字符串的自定义reverse函数返回字符指针和转换指针再次CHAR到一个int使用atoi函数。这是一个简单的方法。

 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define STRMAX 50 

char *reverse(const char *); 

int main(int argc, char **argv){ 
    static char inpBuf[25]; 
    char *ptr = NULL; 
    int num = 1234; 

    /* Convert num to a string */ 
    itoa(num, inpBuf, 10); 

    /* Reverse the string */ 
    ptr = reverse(inpBuf); 

    /* Convert the reversed string back to num */ 
    num = atoi(ptr); 
    /* num is reversed! i.e. 4321 */ 

    /* Free the pointer */ 
    if (ptr) free(ptr); 
} 

char *reverse(const char* sInput) { 
    char* sOutput; 
    int iCnt = 0, iCntRev; 
    sOutput = (char *)malloc((STRMAX * sizeof(char)) + 1); 
    if (sOutput){ 
     for (iCntRev = strlen(sInput); iCntRev >= 0; iCntRev--) { 
      *sOutput++ = sInput[iCntRev]; 
      iCnt++; 
     } 
     *sOutput++ = '\0'; 
    } 
    return (sOutput - iCnt); 
} 

希望这有助于 最好的问候, 汤姆。

+1

在这几行代码中有太多的WTF,它会在free()被调用时崩溃。 – Secure 2010-02-03 14:35:35

+0

@Secure:什么是WTF?在BCC 5.5下工作,OpenWatcom 1.8 ...解释为什么它会在免费被调用时崩溃?这是一个指针malloc'd堆... – t0mm13b 2010-02-03 14:37:02

+0

我从哪里开始?首先,在设置终止时增加sOutput,但不增加iCnt,因此返回的字符串指向malloc的内存的第二个字符。然后,它不能解决问题(打印二进制文件),但会反转一个数字。 Short:通过使用malloc而不是二进制操作进行字符串转换来反转数字。没有主要的回报。免费接受NULL,在测试中没有任何意义。使用sizeof(char),并假设它乘以乘法之外的+1。对sInput使用malloc'ed sOutput和array操作的指针运算。 – Secure 2010-02-03 14:49:19

0

void printbin(int input) int i; int mask = 0x80000000; //假设32位整数 为(I = 0;我< 32;我++){ 如果 (掩模&输入) 的putchar( '1') 别的 的putchar( '0'); mask >> = 1; }}