Twos Complement

Twos Complement project page

Twos Complement demonstrates twos complement arithmetic as implemented in logic registers. Registers operations, including basic arithmetic and logical functions, are implemented with boolean operations without use of numbers.


A BitRegister represents on single-bit register with a value (set or not set) and a carry bit. A TwosComplementRegister uses a collection of single-bit registers to form a register of arbitrary width.


A register is not a number, but its current contents can be converted to an integer, and an integer can be stored in a register. A register can accumulate the results of basic arithmetic, shift, and bitwise operations with the contents of another register, maintaining an overflow flag set by the last operation performed. Overflow status is displayed (with trailing $+) in the register printString.


See unit tests for examples.


17 asRegister ==> a TwosComplementRegister with value 17 (00000000000000000000000000010001)


TwosComplementRegister width: 30 value: SmallInteger maxVal ==> a TwosComplementRegister with value -1 (111111111111111111111111111111)


TwosComplementRegister width: 30 value: SmallInteger maxVal + 1 ==> a TwosComplementRegister with value 0 (000000000000000000000000000000+)


Author: Dave Lewis


License: MIT

TwosComplement-dtl.10.mcz
TwosComplement-dtl.9.mcz
TwosComplement-dtl.8.mcz
TwosComplement-dtl.7.mcz
TwosComplement-dtl.6.mcz
TwosComplement-dtl.5.mcz
TwosComplement-dtl.4.mcz
TwosComplement-dtl.3.mcz
TwosComplement-dtl.2.mcz
TwosComplement-dtl.1.mcz
update-dtl.1.mcm