2010-02-02 55 views
4

我希望能够从键盘输入一个字符并以00000001格式显示所述键的二进制代码。阅读字符的位级

此外,我还想阅读的方式,允许我输出,如果他们是真或假。

例如

01010101 = false,true,false,true,false,true,false,true 

我会后如何我试图做我自己的想法,但我完全不知道,我还在使用C试验,这是在如此低的水平尺度我的编程的第一口味。

三江源

+0

你想在C或C#中使用它吗?您放置的.net3-5标记暗示C#,但您也会放置不一致的C标记。 – 2010-02-02 16:35:36

+0

哎呀对不起,我的意思是C. – 2010-02-02 16:37:32

+0

这是一个有趣的练习! – 2010-02-02 17:02:20

回答

3

这段代码C89:

/* we need this to use exit */ 
#include <stdlib.h> 
/* we need this to use CHAR_BIT */ 
#include <limits.h> 
/* we need this to use fgetc and printf */ 
#include <stdio.h> 

int main() { 
    /* Declare everything we need */ 
    int input, index; 
    unsigned int mask; 
    char inputchar; 

    /* an array to store integers telling us the values of the individual bits. 
     There are (almost) always 8 bits in a char, but it doesn't hurt to get into 
     good habits early, and in C, the sizes of the basic types are different 
     on different platforms. CHAR_BIT tells us the number of bits in a byte. 
    */ 
    int bits[CHAR_BIT]; 

    /* the simplest way to read a single character is fgetc, but note that 
     the user will probably have to press "return", since input is generally 
     buffered */ 
    input = fgetc(stdin); 
    printf("%d\n", input); 

    /* Check for errors. In C, we must always check for errors */ 
    if (input == EOF) { 
     printf("No character read\n"); 
     exit(1); 
    } 

    /* convert the value read from type int to type char. Not strictly needed, 
     we can examine the bits of an int or a char, but here's how it's done. 
    */ 
    inputchar = input; 

    /* the most common way to examine individual bits in a value is to use a 
     "mask" - in this case we have just 1 bit set, the most significant bit 
     of a char. */ 
    mask = 1 << (CHAR_BIT - 1); 

    /* this is a loop, index takes each value from 0 to CHAR_BIT-1 in turn, 
     and we will read the bits from most significant to least significant. */ 
    for (index = 0; index < CHAR_BIT; ++index) { 
     /* the bitwise-and operator & is how we use the mask. 
      "inputchar & mask" will be 0 if the bit corresponding to the mask 
      is 0, and non-zero if the bit is 1. ?: is the ternary conditional 
      operator, and in C when you use an integer value in a boolean context, 
      non-zero values are true. So we're converting any non-zero value to 1. 
     */ 
     bits[index] = (inputchar & mask) ? 1 : 0; 

     /* output what we've done */ 
     printf("index %d, value %u\n", index, inputchar & mask); 

     /* we need a new mask for the next bit */ 
     mask = mask >> 1; 
    } 

    /* output each bit as 0 or 1 */ 
    for (index = 0; index < CHAR_BIT; ++index) { 
     printf("%d", bits[index]); 
    } 
    printf("\n"); 

    /* output each bit as "true" or "false" */ 
    for (index = 0; index < CHAR_BIT; ++index) { 
     printf(bits[index] ? "true" : "false"); 
     /* fiddly part - we want a comma between each bit, but not at the end */ 
     if (index != CHAR_BIT - 1) printf(","); 
    } 
    printf("\n"); 
    return 0; 
} 

你不一定需要三个环路 - 你可以将它们结合在一起,如果你想要的东西,如果你只是做了两种输出之一,然后你不需要这个数组,你可以使用每个位的值来掩盖它。但我认为这使事情分开,希望更容易理解。

+0

+1为彻底的例子,这使我删除我自己的。任何其他人(除非固执或愚蠢)应该做同样的;) – 2010-02-02 17:21:58

+0

非常彻底,不幸的是,当粘贴到一个空白的源文件,我提出了45错误,主要是语法错误和未声明的标识符。 – 2010-02-02 17:37:17

+0

你使用什么编译器? – 2010-02-02 17:51:25

4

这可能不是最安全的方式 - 不理智/尺寸/类型检查 - 但它应该仍然工作。

unsigned char myBools[8]; 
char myChar; 

// get your character - this is not safe and you should 
// use a better method to obtain input... 
// cin >> myChar; <- C++ 
scanf("%c", &myChar); 

// binary AND against each bit in the char and then 
// cast the result. anything > 0 should resolve to 'true' 
// and == 0 to 'false', but you could add a '> 1' check to be sure. 
for(int i = 0; i < 8; ++i) 
{ 
    myBools[i] = ((myChar & (1 << i) > 0) ? 1 : 0); 
} 

这会给你一个无符号字符数组 - 0或1(真或假) - 的字符。

+0

可能应该在(1 << i)前加括号? – 2010-02-02 16:41:48

+0

已编辑 - 但您可以检查它,因为我的C不如C++强。 – acron 2010-02-02 16:44:03

+0

如果你想让scanf读取一个数字,请尝试%d – 2010-02-02 16:49:43

4

对于位调整,使用无符号类型通常更安全,因为带符号负值的移位具有实现相关效应。原始的char可以是有符号的或无符号的(传统上,它在MacIntosh平台上是未签名的,但是在PC上签名)。因此,首先将你的角色投入unsigned char类型。

然后,你的朋友都是按位布尔运算符(&|^~)和移位运算符(<<>>)。例如,如果您的角色在变量x中,那么只需使用:((x >> 5) & 1)即可获得第五位。移位操作符将值向右移动,放下五个低位并将您感兴趣的位移动到“最低位置”(又名“最右边”)。按位AND与1简单地将所有其他位设置为0,因此得到的值为0或1,这是您的位。这里请注意,我从左边的重要(最右边)到最重要(最左边)的位数,我从零开始,而不是一个。

如果你认为你的角色是8位,你可以写你的代码为:

unsigned char x = (unsigned char)your_character; 
int i; 

for (i = 7; i >= 0; i --) { 
    if (i != 7) 
     printf(","); 
    printf("%s", ((x >> i) & 1) ? "true" : "false"); 
} 

你可能注意到,自从我从右到左的位数,但你想输出从左至右,循环索引必须减少。

注意的是,根据C标准,unsigned char具有至少八位但是可以具有多种(现在,仅嵌入DSP极少数具有其不是8位的字符)。为了更加安全,添加此靠近你的代码的开头(作为顶级声明):

#include <limits.h> 
#if CHAR_BIT != 8 
#error I need 8-bit bytes! 
#endif 

这将防止编译成功,如果目标系统恰好是这些特殊的嵌入式DSP之一。作为说明,C标准中的术语“字节”是指“对应于unsigned char”的基本存储器单元,因此在C语言中,一个字节可能具有多于八位(一个字节不是总是一个八位字节)。这是一个混乱的传统来源。

+0

+1,在那里有一些很好的倾向。 – 2010-02-02 17:14:38