6.1.0320 2* two-star CORE

( x1 -- x2 )

x2 is the result of shifting x1 one bit toward the most-significant bit, filling the vacated least-significant bit with zero.

Testing:

T{   0S 2*       ->   0S }T
T{    1 2*       ->    2 }T
T{ 4000 2*       -> 8000 }T
T{   1S 2* 1 XOR ->   1S }T
T{  MSB 2*       ->   0S }T

ContributeContributions

poggingfishavatar of poggingfish [239] Suggested reference implementation2022-06-20 17:34:22

: 2* 2 * ;
Reply New Version

CandidMoeavatar of CandidMoe [366] Bad test caseExample2024-11-01 20:33:42

The test

T{ MSB 2* -> 0S }T

fails, because the right result is -2, not 0.

AntonErtlavatar of AntonErtl

MSB is the value where only the most significant bit is set:

0 INVERT CONSTANT 1S
1S 1 RSHIFT INVERT CONSTANT MSB
msb h. \ output on a 64-bit system: $8000000000000000  ok
msb 2* . \ output: 0  ok

For Forth-2012 one might argue that this case is an overflow of an arithmetic operation and therefore the result should be implementation-defined (so such a test case should not exist), but 2* is specified as a shift (i.e., a bitwise operation), and for a shift the most significant bit should clearly be shifted out by 2*.

Closed
Reply New Version