GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | NAND

Edit History

NAND

y = a NAND b

Home > Reference > Language

Bitwise exclusive NOT AND.

Truth table:

a b a NAND b
0 0 1
0 1 1
1 0 1
1 1 0

Example 1

The NOT-operation as part of NAND performs a bitwise inversion on all bits of a number. This leads to the following (maybe unexpected) result:

print 1 NAND 1   ' Output: 11111111111111111111111111111110

Example 2: Operate NAND only on last n bits

If you want to operate NAND only on the last n bits of the numbers, you can use the following code:

n = 4
a = 0b1101
b = 0b1001

print bin((a NAND b) BAND ((1 lshift n ) - 1)) 

' Output 110

Example 3: Two’s complement

Two’s complement is a mathematical operation to reversibly convert a positive binary number into a negative binary number with equivalent negative value. (Wikipedia)

' Two's complement is the standard way of representing negative integers in binary. 
' The sign is changed by inverting all of the bits and adding one.

Def invsgn(n) = ((n Nand n) + 1) - Frac(n)              ' invert the sign of n

While True Do
  Input "Enter a number (Enter empty to stop) : ", n
  If Isstring(n) Then
    Stop
  Endif
  
  Print "This is the number with inverted sign: "; invsgn(n)
  Print
Wend
Code samples using NAND
klotski.bas   klotski.bas   klotski_puzzle.bas  
Language
AND   AS   BAND   BG   BOR   BYREF   CALL   CASE   CATCH   CONST   DECLARE   DEF   DO   ELIF   ELSE   ELSEIF   END   END TRY   ENDIF   EQV   EXIT   FALSE   FI   FOR   FUNC   GOSUB   GOTO   IF   IFF   IMP   IN   LABEL   LET   LIKE   LOCAL   LSHIFT   MDL   MOD   NAND   NEXT   NOR   NOT   ON   OR   REM   REPEAT   RETURN   RSHIFT   SELECT   STEP   STOP   SUB   THEN   THROW   TO   TRUE   TRY   UNTIL   USE   USG   USING   WEND   WHILE   XNOR   XOR  
This page was last edited on Wed, 7 Jun 2023 23:30:12 +0200 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.