-2

I'm trying to generate a truth table of "integers". First of all I need to have 2 lists of integers just like this table right here:

1 1   
1 2
2 1
2 2

And then I need to use Boolean operators to generate the table like this:

1 1 2 1 2 2
1 2 1 2 2 1
2 1 1 2 1 2
2 2 2 1 1 1

I checked out some related questions like: Generating truth tables in Java or boolean operations with integers but I still don't know how to write it in VB.net. So I appreciate your help and time. :) Thank you!

Community
  • 1
  • 1
Behnood
  • 11
  • 4
  • 3
    I don't understand the logic of your truth-table. – Tim Schmelter Mar 04 '14 at 13:39
  • Which part of this task are you having trouble with? For instance, are you having difficulty generating the integers, or doing the boolean operations, or outputing the table of numbers or results? – Steven Doggart Mar 04 '14 at 13:40
  • the logic of my table is: if x=1 then true and if x=2 then false. I don't know how to make the lists and generate the table. The Boolean operations are xor, xnor and not. – Behnood Mar 04 '14 at 13:49
  • Typically, 0 is false and 1 (or non-zero) is true. You certainly chose some weird numbers to represent this. – Douglas Barbin Mar 04 '14 at 13:58
  • So, what have you tried and what *specifically* are you having trouble with? What about what you tried didn't work? It's still very unclear what you are asking, unless you are just asking someone to write all the code for you, that is, which is frowned upon. – Steven Doggart Mar 04 '14 at 14:04
  • I just knew the algorithm, and I'm very new to vb.net. – Behnood Mar 04 '14 at 14:17

1 Answers1

1

I am assuming that you want to Xor the boolean values and not the binary representations of the integers themselves. So my answer is based on that assumption.

If you are using 1 for True and 2 for False, then I would suggest that you write some conversion functions.

Private Function IntegerToBoolean(number As Integer) As Boolean
    Return If(number = 1, True, False)
End Function

Private Function BooleanToInteger(bool As Boolean) As Integer
    Return If(bool, 1, 2)
End Function

then, using those, it's fairly trivial to write your other functions:

Private Function IntegerXor(int1 As Integer, int2 As Integer) As Integer
    Dim bool1 As Boolean = IntegerToBoolean(int1)
    Dim bool2 As Boolean = IntegerToBoolean(int2)
    Dim boolResult as Boolean = (bool1 Xor bool2)
    Return BooleanToInteger(boolResult)
End Function

etc.

Obviously you would do this for each of your numbers in your table, and you would create additional functions for And and Or.

Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34