bits

package module
v0.0.0-...-df86a3f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2018 License: MIT Imports: 6 Imported by: 1

README

bits

Manipulate bits and truth tables.

Example for reversing xor:

package main

import (
	"fmt"
	"github.com/xyproto/bits"
)

func main() {
	// Define the XOR function with a TruthTable
	xor := &bits.TruthTable{
		"0 0 -> 0",
		"0 1 -> 1",
		"1 0 -> 1",
		"1 1 -> 0",
	}

	// This program reverses XOR from 0 to 1, 1 without trying all 4 possible inputs
	to := bits.Bits{1, 1} // Could be 0 0, 0 1, 1 0 or 1 1
	from := xor.Process(to) // The output of the xor function given 1 1 is 0

	// Create a validator function
	validator := func (input *bits.Bits) bool {
		return input.Equal(&to)
	}

	// Reverse the xor function and find the input bits, as confirmed by the validator
	foundInput, iterations, err := bits.Reverse(xor, from, validator)
	if err != nil {
		panic(err)
	}

	// Output info
	fmt.Printf("Reversed xor in %d iterations (instead of %d) and found %s given %s\n", iterations, len(*xor), foundInput, from)
}

License: MIT

Documentation

Index

Constants

View Source
const (
	B0 = Bit(0)
	B1 = Bit(1)
)

Shortcuts for bits. Remember to compare a Bit with a Bit, not with an int!

Variables

This section is empty.

Functions

func ValidRow

func ValidRow(ttRow string) bool

Check if a row in a truth table has valid syntax

Types

type Bit

type Bit int // bit, as integer

A new Gate is defined by a truth table, or by a choicetable

func NewBit

func NewBit(s string) Bit

Convert a "0" or "1" to a single bit

func (Bit) String

func (b Bit) String() string

Convert a single bit to a string

type Bits

type Bits []Bit // input bits, or output bits

A new Gate is defined by a truth table, or by a choicetable

func Reverse

func Reverse(tt *TruthTable, output Bit, vf ValidatorFunc) (foundInput *Bits, iterations uint, err error)

Given a truth table and an output bit, reverse the output and find the input. Also needs a ValidatorFunc to return true when the correct input has been found. Returns the found input, number of iterations that were required and an error if not found

func StringToBits

func StringToBits(s string) (*Bits, error)

StringToBits converts a space-separated string of "0"s and "1"s to a pointer to a slice of bits.Bit. Returns an error if an invalid string is given.

func (*Bits) Equal

func (b *Bits) Equal(x *Bits) bool

Check if two slices of Bit are equal

func (Bits) String

func (b Bits) String() string

Take a slice of ints (0 or 1), return a space separated string ("0 1 0 1 0")

type Choices

type Choices []Bits

Multiple possible outputs, with equal probability

func NewChoices

func NewChoices(s string) *Choices

Takes a string like this: "0 0 | 0 1 | 1 0 | 1 1" and returns the four elements (separated by "|") as *Choices (slice of Bits)

func StringToChoices

func StringToChoices(s string) Choices

Takes a string like this: "0 0 | 0 1 | 1 0 | 1 1" and returns the four elements (separated by "|") as Choices (slice of Bits)

type Gater

type Gater interface {
	Process(interface{}) interface{}
}

A gate must have a Process function, that can take Input or Inputs, and return Output or Outputs

type ManyToManyGate

type ManyToManyGate func(Bits) Bits

A ManyToManyGate can have several inputs and outputs

func (*ManyToManyGate) Process

func (mg *ManyToManyGate) Process(inputOrInputs interface{}) (outputOrOutputs interface{})

Multi can act as a gate (Gater) many to many

type MultiProbGate

type MultiProbGate func(Bits) Choices

A probgate with many inputs and several possible outputs Example prob table: {"0 1 1 0 1 -> 1 1 | 0 0"}, would return Choices{Outputs{1, 1}, Outputs{0, 0}} if given 0

func (*MultiProbGate) Process

func (cg *MultiProbGate) Process(inputOrInputs interface{}) (outputOrOutputs interface{})

Multi Choice Gate can act as a gate (Gater) many bits to many possible choices

type OneToManyGate

type OneToManyGate func(Bits) Bit

A logic gate with multiple inputs and one output

func NewOneToManyGate

func NewOneToManyGate(tt *TruthTable) *OneToManyGate

Create a new logic gate, like "and" or "xor", with inputs and one output

func (*OneToManyGate) Process

func (lg *OneToManyGate) Process(inputOrInputs interface{}) (outputOrOutputs interface{})

OneToManyGate can act as a gate (Gater) many to one

type OneToOneGate

type OneToOneGate func(Bit) Bit

A logic gate (like "not")

func (*OneToOneGate) Process

func (ug *OneToOneGate) Process(inputOrInputs interface{}) (outputOrOutputs interface{})

OneToOneGate can act as a gate (Gater) one to one

type ProbGate

type ProbGate func(Bit) Choices

A simple probgate with one input and several possible outputs Example prob table: {"0 -> 1 1 | 0 0"}, would return Choices{Outputs{1, 1}, Outputs{0, 0}} if given 0

func NewProbGate

func NewProbGate(pt *ProbTable) *ProbGate

Create a new probability gate, a ProbGate, where one input can give many alternative outputs

func (*ProbGate) Process

func (scg *ProbGate) Process(inputOrInputs interface{}) (outputOrOutputs interface{})

Choice Gate can act as a gate (Gater) one bit to many possible choices

type ProbTable

type ProbTable []string

func (*ProbTable) Gate

func (pt *ProbTable) Gate() ProbGate

Take prob table, return a prob gate

func (*ProbTable) Process

func (pt *ProbTable) Process(input Bit) Choices

ProbTable can act as a gate (Gater), one to many different choices

func (*ProbTable) String

func (pt *ProbTable) String() string

type TruthTable

type TruthTable []string

func (*TruthTable) Complete

func (tt *TruthTable) Complete() bool

Check if a truth table is complete If tables are assumed to not contain duplicates, one could just count the rows too

func (*TruthTable) Gate

func (tt *TruthTable) Gate() OneToManyGate

Take truth table, return a Gate

func (*TruthTable) Invert

func (tt *TruthTable) Invert() *ProbTable

Make it possible to run a truth table in reverse, by returning a table with probabilities

func (*TruthTable) Process

func (tt *TruthTable) Process(inputs Bits) Bit

TruthTable can act as a gate (Gater), many to one

func (*TruthTable) String

func (tt *TruthTable) String() string

type ValidatorFunc

type ValidatorFunc func(*Bits) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL