在 C 中模拟逻辑门的最佳方法?

Best Way to Simulate Logic Gates in C?

你好我想知道如果我想在 c 程序中模拟逻辑门,是否有人能够向我解释最好的方法是什么? 比方说我创建了一个程序并使用命令行参数

AND GATE
[console]% yourProgram 11001010 11110000
<console>% 11000000

如果有人能向我解释最好的路线是什么,我将不胜感激。这是我到目前为止的代码...

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

int main( int argc, char *argv[] ) {
        if( argc >= 3){
                int result = atoi(argv[1])&&atoi(argv[2]);
                printf("Input 1 is %d\n",atoi(argv[1]));
                printf("Input 2 is %d\n",atoi(argv[2]));
                printf("Result is %c\n",result);
        }
        return 0;

使用此代码。 (对于 AND 运算):

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

int main( int argc, char *argv[] ) {
    if( argc >= 3){
        int i=0;
        printf("1st i/p = %s\n2nd i/p = %s\n",argv[1],argv[2]);
        for (i=0; argv[1][i]!='[=10=]'; i++){   //this assumes there are 2 inputs, of equal size, having bits(1,0) as its digits
            argv[1][i] = argv[1][i] & argv[2][i];    //modifies argv[1] to your required answer
        }
        printf("Answer: %s\n",argv[1]);
    }
    return 0;
}

除了建议基本更正的评论之外,如果你想让它更有用和灵活一点,你可以计算 most significant bit 然后用它来格式化一个简单的二进制打印例程来检查你的位运算。

主要概念是将输入作为一串二进制数字并将其转换为具有 strtoul(基数 2)的数字,然后将输入 &'ing 在一起以获得result 这只是计算要打印多少字节以及是将单个字节格式化为半字节还是简单地分隔多个字节的问题。

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

/* BUILD_64 */
#if defined(__LP64__) || defined(_LP64)
# define BUILD_64   1
#endif

/* BITS_PER_LONG */
#ifdef BUILD_64
# define BITS_PER_LONG 64
#else
# define BITS_PER_LONG 32
#endif

/* CHAR_BIT */
#ifndef CHAR_BIT
# define CHAR_BIT  8
#endif

char *binstrfmt (unsigned long n, unsigned char sz, unsigned char szs, char sep);
static __always_inline unsigned long msbfls (unsigned long word);

int main (int argc, char **argv) {

    if ( argc < 3) {
        fprintf (stderr, "error: insufficient input. usage: %s b1 b1\n", argv[0]);
        return 1;
    }

    /* input conversion and bitwise operation */
    unsigned long b1 = strtoul (argv[1], NULL, 2);
    unsigned long b2 = strtoul (argv[2], NULL, 2);
    unsigned long result = b1 & b2;

    /* variables to use to set binary print format */
    unsigned char msb, msbmax, width, sepwidth;
    msb = msbmax = width = sepwidth = 0;

    /* find the greatest most significant bit */
    msbmax = (msb = msbfls (b1)) > msbmax ? msb : msbmax;
    msbmax = (msb = msbfls (b2)) > msbmax ? msb : msbmax;
    msbmax = (msb = msbfls (result)) > msbmax ? msb : msbmax;
    msbmax = msbmax ? msbmax : 1;

    /* set the number of bytes to print and the separator width */
    width = (msbmax / CHAR_BIT + 1) * CHAR_BIT;
    sepwidth = width > CHAR_BIT ? CHAR_BIT : CHAR_BIT/2;

    /* print the output */
    printf("\n Input 1 : %s\n", binstrfmt (b1, width, sepwidth, '-'));
    printf(" Input 2 : %s\n", binstrfmt (b2, width, sepwidth, '-'));
    printf(" Result  : %s\n\n", binstrfmt (result, width, sepwidth, '-'));

    return 0;
}

/** returns pointer to formatted binary representation of 'n' zero padded to 'sz'.
 *  returns pointer to string contianing formatted binary representation of
 *  unsigned 64-bit (or less ) value zero padded to 'sz' digits with char
 *  'sep' placed every 'szs' digits. (e.g. 10001010 -> 1000-1010).
 */
char *binstrfmt (unsigned long n, unsigned char sz, unsigned char szs, char sep) {

    static char s[2 * BITS_PER_LONG + 1] = {0};
    char *p = s + 2 * BITS_PER_LONG;
    unsigned char i;

    for (i = 0; i < sz; i++) {
        p--;
        if (i > 0 && szs > 0 && i % szs == 0)
            *p-- = sep;
        *p = (n >> i & 1) ? '1' : '0';
    }

    return p;
}

/* return the most significant bit (MSB) for the value supplied. */
static __always_inline unsigned long msbfls(unsigned long word)
{
    if (!word) return 0;

    int num = BITS_PER_LONG - 1;

#if BITS_PER_LONG == 64
    if (!(word & (~0ul << 32))) {
        num -= 32;
        word <<= 32;
    }
#endif
    if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
        num -= 16;
        word <<= 16;
    }
    if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
        num -= 8;
        word <<= 8;
    }
    if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
        num -= 4;
        word <<= 4;
    }
    if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
        num -= 2;
        word <<= 2;
    }
    if (!(word & (~0ul << (BITS_PER_LONG-1))))
        num -= 1;

    return num;
}

示例输出

$ ./bin/andargs 11001010 11110000

 Input 1 : 1100-1010
 Input 2 : 1111-0000
 Result  : 1100-0000

$ ./bin/andargs 1100101011110000 1111000011001010

 Input 1 : 11001010-11110000
 Input 2 : 11110000-11001010
 Result  : 11000000-11000000