Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top