6.1.1720 INVERT CORE

( x1 -- x2 )

Invert all bits of x1, giving its logical inverse x2.

See:

Rationale:

The word NOT was originally provided in Forth as a flag operator to make control structures readable. Under its intended usage the following two definitions would produce identical results:

: ONE ( flag -- )
   IF ." true" ELSE ." false" THEN ;

: TWO ( flag -- )
   NOT IF ." false" ELSE ." true" THEN ;

This was common usage prior to the Forth-83 Standard which redefined NOT as a cell-wide one's-complement operation, functionally equivalent to the phrase -1 XOR. At the same time, the data type manipulated by this word was changed from a flag to a cell-wide collection of bits and the standard value for true was changed from "1" (rightmost bit only set) to "-1" (all bits set). As these definitions of TRUE and NOT were incompatible with their previous definitions, many Forth users continue to rely on the old definitions. Hence both versions are in common use.

Therefore, usage of NOT cannot be standardized at this time. The two traditional meanings of NOT — that of negating the sense of a flag and that of doing a one's complement operation — are made available by 0= and INVERT, respectively.

Testing:

T{ 0S INVERT -> 1S }T
T{ 1S INVERT -> 0S }T

ContributeContributions

Budsyavatar of Budsy [53] Successful test cases would be goodSuggested Testcase2018-02-28 05:03:11

t{ 05 invert -> -06 }T t{ -06 invert -> 05 }T

Budsyavatar of BudsyNew Version: Successful test cases would be good

Hide differences

t{ 05 invert -> -06 }T t{ -06 invert -> 05 }T

My comment was based on a misreading of the given test cases. These looked like the number 05 rather than the constant 0S Boolean value provided in the test lexicon.

AntonErtlavatar of AntonErtl

These test cases have an environmental dependency on negative numbers being in twos-complement format, which every system implements, but Forth-2012 did not standardize. We have standardized 2s-complement afterwards, so this test case will not have an environmental dependency in the next standard.

Reply New Version