Skip to content

Commit

Permalink
Reverse bit order in Truth.Val
Browse files Browse the repository at this point in the history
I.e. now the 0th bit corresponds to 'z' and the 51st bit corresponds to
'A' if every letter is used.

This allows us to display each atomic in alphabetical order in a truth
table yet still have the rows count up as if they were a binary number
(i.e. {0-0, 0-1, 1-0, 1-1} order instead of {0-0, 1-0, 0-1, 1-1}, which
is what it was before), yet still be able to iterate through all the
truth value sets via incrementing Truth.Val.
  • Loading branch information
Ro5bert committed Oct 2, 2019
1 parent eb3fd4b commit 501635c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
9 changes: 6 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type Truth struct {
Names []byte
}

// TODO: fix (reverse) shift mapping

func (t Truth) get(stmt byte) bool {
return t.Val&(1<<t.shiftMap[alphaToIdx(stmt)]) > 0
}
Expand Down Expand Up @@ -54,7 +52,12 @@ func newTruth(atomics uint64) Truth {
var shiftMap [52]byte
names := make([]byte, 0, 52)
var shift byte
for i := byte(0); i < 52; i++ {
// Here we count down from 51 instead of up from 0 to effectively reverse the bit order in Truth.Val.
// This allows us to display each atomic in alphabetical order in a truth table and not have the rows "appear
// backwards" (e.g. {0-0, 1-0, 0-1, 1-1} instead of {0-0, 0-1, 1-0, 1-1}) yet still be able to count up by simply
// incrementing Truth.Val.
// Less than 52 in condition because of wrap around.
for i := byte(51); i < 52; i-- {
if atomics&(1<<i) > 0 {
shiftMap[i] = shift
shift++
Expand Down
6 changes: 3 additions & 3 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ func TestParseEval(t *testing.T) {
{"a&b", []bool{false, false, false, true}},
{"a|b", []bool{false, true, true, true}},
{"a^b", []bool{false, true, true, false}},
{"a>b", []bool{true, false, true, true}},
{"a>b", []bool{true, true, false, true}},
{"a=b", []bool{true, false, false, true}},
{"0", []bool{false}},
{"1", []bool{true}},
{"a|!0", []bool{true, true}},
{"(a & b)", []bool{false, false, false, true}},
{"!(a > b)", []bool{false, true, false, false}},
{"!(a > b)", []bool{false, false, true, false}},
{"!!(a = b)", []bool{true, false, false, true}},
{"(a = b) | b", []bool{true, false, true, true}},
{"(a = b) | b", []bool{true, true, false, true}},
} {
stmt, truth, err := Parse(c.input)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions truthtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func printLine(nAtomics int, outputWidth int, out io.Writer, rowSep string, l st
func printHeader(atomics []byte, stmt string, out io.Writer, cs *CharSet) error {
var sb strings.Builder
sb.Grow(calcInputWidth(len(atomics)))
for i, a := range atomics {
sb.WriteByte(a)
if i < len(atomics)-1 {
for i := len(atomics) - 1; i >= 0; i-- {
sb.WriteByte(atomics[i])
if i > 0 {
sb.WriteString(" ")
}
}
Expand All @@ -113,9 +113,9 @@ func colourize(val bool) string {

func printData(truth uint64, nAtomics int, output bool, outputWidth int, out io.Writer, cs *CharSet) error {
var sb strings.Builder
for i := 0; i < nAtomics; i++ {
for i := nAtomics - 1; i >= 0; i-- {
sb.WriteString(colourize(truth&(1<<i) > 0))
if i < nAtomics-1 {
if i > 0 {
sb.WriteString(" ")
}
}
Expand Down

0 comments on commit 501635c

Please sign in to comment.