真值表输入生成 [英] Truth table input generation

查看:393
本文介绍了真值表输入生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定的n个输入,我需要用C ++生成所有可能的输入组合

 如。 N = 4我需要得到,10101010101010101100110011001100111100001111000011111111亿

(编辑:如果这是不明确,这些都是输入组合逐列读)

我需要这些,来执行和操作的放大器;和|所以如果我让他们在整复presentation为n个不同的变量,这将是最好的。

我尝试了使用bitset的32输入组合做,但它花了很长的时间来处理。我希望如果你们对更好地实施任何想法?

 编辑:例如,当N = 3101010101100110011110000


解决方案

下面是一个简短的实现,它生成的输出:

 无效print_mask(IN​​T N){
    对于(INT水平= 0;&级LT; N;水平++){
        的for(int i =(1 <<;&LT; N)-1; I&GT; = 0;我 - )//我们将永远输出2 ** n位
            的printf(%D中,(i&GT;&gt;级)及1);
        的printf(\\ n);
    };
};

For given n inputs, I need to generate all possible input combinations using C++

eg. n =4

I need to get,

1010101010101010

1100110011001100

1111000011110000

1111111100000000

(EDIT : In case this is not clear, these are the input combinations read column-wise)

I need these to perform operations like & and | so it would be best if I get them in their integer representation as n different variables.

I tried doing it using bitset for 32 input combinations but it took a long time to process. I was hoping if you guys had any ideas on a better implementation?

EDIT : Example when n=3

10101010

11001100

11110000

解决方案

Here is a short implementation that generates that output:

void print_mask(int n){
    for (int level = 0; level < n; level++){
        for (int i = (1<<n)-1; i>=0; i--)   // we'll always output 2**n bits
            printf("%d", (i >> level) & 1);
        printf("\n");
    };
};

这篇关于真值表输入生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆