# Unary Operators

Unary operators work with a single argument. Click on any operator in the list below for details and examples.

(See also: Binary Operators and Operator Precedence.)

## Notes about the 16-bit workspace

Many of the operator descriptions will say something like "computes (some function) of a 16-bit number." This does not mean that the operator does not work on smaller byte or nibble values, but rather that the computation is done in a 16-bit workspace. If the value is smaller than 16 bits, the BASIC Stamp pads it with leading 0s to make a 16-bit value. If the 16-bit result of a calculation is to be packed into a smaller variable, the higher-order bits are discarded (truncated).

ABS (Returns Absolute Value)

The Absolute Value operator (ABS) converts a signed (two’s complement) 16-bit number to its absolute value. The absolute value of a number is a positive number representing the difference between that number and 0. For example, the absolute value of -99 is 99. The absolute value of 99 is also 99. ABS works on two’s complement negative numbers. Here's an example of ABS at work:

    result  VAR     Word

    Main:
      result = -99
      DEBUG SDEC ? result                           ' Display -99
      DEBUG SDEC ? ABS result                       ' Display 99
      END

- (Negate)

The Negative operator (-) negates a 16-bit number (converts to its two’s complement).

    SYMBOL  result = W1

    Main:
      result = -99                                  ' Initialize result
      result = result + 100                         ' Add 100 to it
      DEBUG result                                  ' Display result (1)
      END

    result  VAR     Word

    Main:
      result = -99                                  ' Initialize result
      DEBUG SDEC result                             ' Display result (-99)
      result = result + 100                         ' Add 100 to it
      DEBUG SDEC result                             ' Display result (1)
      END

~ (Invert Bits)

The Inverse operator (~) complements (inverts) the bits of a number. Each bit that contains a 1 is changed to 0 and each bit containing 0 is changed to 1. This process is also known as a "bitwise NOT" and one's complement.

~0 = 1 ~1 = 0

    result  VAR     Byte

    Main:
      result = %11110001                            ' Store bits in result
      DEBUG BIN8 ? result                           ' Display %11110001
      result = ~result                              ' Complement result
      DEBUG BIN8 ? result                           ' Display %00001110
      END

DCD (2n power Decoder)

The Decoder operator (DCD) is a 2n-power decoder of a four-bit value. DCD accepts a value from 0 to 15, and returns a 16-bit number with the bit, described by value, set to 1.

    result  VAR     Word

    Main:
      result = DCD 12                               ' Set bit 12.
      DEBUG BIN ? result                            ' Display %0001000000000000
      END

NCD (Priority Encoder of 16-bit Value)

The Encoder operator (NCD) is a "priority" encoder of a 16-bit value. NCD takes a 16-bit value, finds the highest bit containing a 1 and returns the bit position plus one (1 through 16). If no bit is set (the input value is 0) NCD returns 0. NCD is a fast way to get an answer to the question "What is the largest power of two that this value is greater than or equal to?" The value NCD returns will be that power, plus one.

    result  VAR     Nib

    Main:
      result = %1101                                ' Highest bit set is bit 3
      DEBUG ? NCD result                            ' Show the NCD of Result (4)
      END

SIN (Returns signed Sine of Angle)

The Sine operator (SIN) returns the two’s complement, 16-bit Sine of an angle specified as an 8-bit (0 to 255) value. To understand the SIN operator more completely, let’s look at a typical Sine function. By definition: given a circle with a radius of 1 unit (known as a unit circle), the Sine is the y-coordinate distance from the center of the circle to its edge at a given angle. Angles are measured relative to the 3-o'clock position on the circle, increasing as you go around the circle counterclockwise.

At the origin point (0 degrees) the Sine is 0, because that point has the same y (vertical) coordinate as the circle center. At 45 degrees the Sine is 0.707. At 90 degrees, Sine is 1. At 180 degrees, Sine is 0 again. At 270 degrees, Sine is -1.

The BASIC Stamp SIN operator breaks the circle into 0 to 255 units instead of 0 to 359 degrees. Some textbooks call this unit a binary radian or brad. Each brad is equivalent to 1.406 degrees. And instead of a unit circle, which results in fractional Sine values between 0 and 1, BASIC Stamp SIN is based on a 127-unit circle. Results are given in two’s complement form in order to accommodate negative values. So, at the origin, SIN is 0. At 45 degrees (32 brads), Sine is 90. At 90 degrees (64 brads), Sine is 127. At 180 degrees (128 brads), Sine is 0 again. At 270 degrees (192 brads), Sine is -127.

See the image below for the relationship between degrees and brads and how the SIN function values relate to to the 127-unit circle.

    degr    VAR     Word
    sine    VAR     Word

    Main:
      FOR degr = 0 TO 359 STEP 45                   ' Use degrees
        sine = SIN (degr * 128 / 180)               ' Convert to brads, do SIN
        DEBUG "Angle: ", DEC degr, TAB              ' Display results
        DEBUG "Sine:  ", SDEC sine, CR
      NEXT
      END

COS (Returns signed Cosine of Angle)

The Cosine operator (COS) returns the two’s complement, 16-bit Cosine of an angle specified as an 8-bit (0 to 255) value. To understand the COS operator more completely, let’s look at a typical Cosine function. By definition: given a circle with a radius of 1 unit (known as a unit circle), the Cosine is the x-coordinate distance from the center of the circle to its edge at a given angle. Angles are measured relative to the 3-o'clock position on the circle, increasing as you go around the circle counterclockwise.

At the origin point (0 degrees) the Cosine is 1, because that point has the same x (horizontal) coordinate as the circle radius. At 45 degrees the Cosine is 0.707. At 90 degrees, Cosine is 0. At 180 degrees, Cosine is -1. At 270 degrees, Cosine is 0 again.

The BASIC Stamp COS operator breaks the circle into 0 to 255 units instead of 0 to 359 degrees. Some textbooks call this unit a binary radian or brad. Each brad is equivalent to 1.406 degrees. And instead of a unit circle, which results in fractional Cosine values between 0 and 1, BASIC Stamp COS is based on a 127-unit circle. Results are given in two’s complement form in order to accommodate negative values. So, at the origin, COS is 127. At 45 degrees (32 brads), COS is 90. At 90 degrees (64 brads), COS is 0. At 180 degrees (128 brads), Cosine is -127. At 270 degrees (192 brads), Cosine is 0 again.

See the image below for the relationship between degrees and brads and how the COS function values relate to to the 127-unit circle.

    degr    VAR     Word
    cosine  VAR     Word

    Main:
      FOR degr = 0 TO 359 STEP 45                   ' Use degrees
        cosine = COS (degr * 128 / 180)             ' Convert to brads, do COS
        DEBUG "Angle:  ", DEC degr, TAB             ' Display results
        DEBUG "Cosine: ", SDEC cosine, CR
      NEXT
      END

SQR (Returns Square Root of Value)

The Square Root operator (SQR) computes the integer square root of an unsigned 16-bit number. (The number must be unsigned since the square root of a negative number is an 'imaginary' number.) Remember that most square roots have a fractional part that the BASIC Stamp discards when doing its integer-only math. So it computes the square root of 100 as 10 (correct), but the square root of 99 as 9 (the actual is close to 9.95).

    Main:
      DEBUG SQR 100                                 ' Display 10
      DEBUG SQR 99                                  ' Display 9 (due to truncation)
      END

NOT (Logical NOT)

The NOT operator returns the logical NOT of an expression. Note that in the BASIC Stamp, a non-zero value is considered True (T), zero is considered False (F). The expression is subject to the following logic:

`NOT F = T NOT T = F`

The result returned by NOT will be True or False.

    speed   VAR     Byte

    Main:
      DEBUG CLS, "Enter speed: "
      DEBUGIN DEC speed
      IF NOT (speed < 65) THEN                      ' speed must be less than 65
        DEBUG CR, "Sorry ... too fast!"
      ELSE
        DEBUG CR, "Enjoy your trip."
      ENDIF
      END

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012