larspensjo / go-monotonic-graycode

Library for generation of Monotonic Gray Codes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Monotonic Gray Codes

Gray codes are sequences of binary numbers where only one bit change between every subsequent number. E.g. a 4 bit number can be:

0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000

Monotonic Gray codes also have the property that the total number of bits increase with the value of the number. E.g. a 4 bit bit numbers can be:

0000 0001 0011 0010 0110 0100 1100 1000 1010 1011 1001 1101 0101 0111 1111 1110

Implementation is based on http://sciyoshi.com/2010/12/gray-codes/.

Copyright and licensing

Distributed under GNU Lesser General Public License.

Usage

Initialize with the number of bits. This must be done before any other functions are used.

	import 	"github.com/larspensjo/go-monotonic-graycode"
	numbers := mgc.New(numBits)

To get the Monotonic Gray Code for the integer 'n':

	numbers.GetMgc(n)

To convert a Monotonic Gray Code 'c' back to an integer:

	numbers.GetInt(c)

Signed numbers

This library only handles unsigned numbers. One way to use signed numbers, is to use a transformation. E.g. having the 16-bit MGC number 0-0x7FFF represent positive numbers as is, and 0x8000-0xFFFF represent the number -32768 to -1, respectively.

About

Library for generation of Monotonic Gray Codes


Languages

Language:Go 100.0%