This repository has been archived on 2024-09-22. You can view files and clone it, but cannot push or open issues or pull requests.
pbi-ide/help/BasicStampHelp/Content/LanguageTopics/Reference/Ops_uni.htm

307 lines
21 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="PBASIC Language Reference|PBASIC Operators" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
<head><title>Unary Operators</title>
<link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
<link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
<script src="../../SkinSupport/MadCapAll.js">
</script>
</head>
<body>
<div class="MCBreadcrumbsBox_0"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="AlphaRef.htm">PBASIC Language Reference</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="Operators.htm">PBASIC Operators</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Unary Operators</span>
</div>
<h1>Unary Operators</h1>
<p>
<img src="../../graphics/pgm_icon1.gif" border="0" alt="" />
<img src="../../graphics/pgm_icon2.gif" border="0" alt="" />
<img src="../../graphics/pgm_icon2e.gif" border="0" alt="" />
<img src="../../graphics/pgm_icon2sx.gif" border="0" alt="" />
<img src="../../graphics/pgm_icon2p.gif" border="0" alt="" />
<img src="../../graphics/pgm_icon2pe.gif" border="0" alt="" />
<img src="../../graphics/pgm_icon2px.gif" border="0" alt="" />
</p>
<p>&#160;</p>
<p class="PlainText">Unary operators work with a single argument. Click on any operator in the list below for details and examples.</p>
<p class="PlainText">(See also: <a href="Ops_bi.htm" target="" title="" alt="" class="MCXref_0">Binary Operators</a> and <a href="OpsPrecedence.htm" target="" title="" alt="" class="MCXref_0">Operator Precedence</a>.)</p>
<h2>Notes about the 16-bit workspace</h2>
<p class="PlainText">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).</p>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_0" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />ABS&#160;&#160;&#160;(Returns Absolute Value)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_0" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="absval"></a>The Absolute Value operator (ABS) converts a signed (twos 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 twos complement negative numbers. Here's an example
of ABS at work:</p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
result VAR Word
Main:
result = -99
DEBUG SDEC ? result ' Display -99
DEBUG SDEC ? ABS result ' Display 99
END
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_1" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />- &#160;&#160;(Negate)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_1" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="negative"></a>The Negative operator (-) negates a 16-bit number (converts to its twos
complement).</p>
<img src="../../graphics/bs1_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
SYMBOL result = W1
Main:
result = -99 ' Initialize result
result = result + 100 ' Add 100 to it
DEBUG result ' Display result (1)
END
</pre>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
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
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_2" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />~&#160;&#160;&#160;(Invert Bits)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_2" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="invert"></a>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.</p>
<p class="PlainText"><span class="code_in_text">&#160; &#160; ~0 = 1
&#160; &#160; ~1 = 0</span>
</p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
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
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_3" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />DCD &#160;&#160;(2<sup>n</sup> power Decoder)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_3" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="dcd"></a>The Decoder operator (DCD) is a 2<sup>n</sup>-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.</p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
result VAR Word
Main:
result = DCD 12 ' Set bit 12.
DEBUG BIN ? result ' Display %0001000000000000
END
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_4" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />NCD &#160;&#160;(Priority Encoder of 16-bit Value)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_4" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="ncd"></a>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.</p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
result VAR Nib
Main:
result = %1101 ' Highest bit set is bit 3
DEBUG ? NCD result ' Show the NCD of Result (4)
END
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_5" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />SIN &#160;&#160;(Returns signed Sine of Angle)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_5" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="sine"></a>The Sine operator (SIN) returns the twos complement, 16-bit Sine of an
angle specified as an 8-bit (0 to 255) value. To understand the SIN operator
more completely, lets 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.</p>
<p class="PlainText">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.</p>
<p class="PlainText">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 twos 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.</p>
<p class="PlainText">See the image below for the relationship between degrees and brads and how the SIN
function values relate to to the 127-unit circle.</p>
<p class="PlainText" style="text-align: center;">
<img src="../../graphics/SIN_COS.png" />
</p>
<center>&#160;</center>
<p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" />
</p><pre class="BScode" xml:space="preserve">
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
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_6" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />COS &#160;&#160;(Returns signed Cosine of Angle)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_6" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="cosine"></a>The Cosine operator (COS) returns the twos complement, 16-bit Cosine of an
angle specified as an 8-bit (0 to 255) value. To understand the COS operator
more completely, lets 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.</p>
<p class="PlainText">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.</p>
<p class="PlainText">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 twos 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.</p>
<p class="PlainText">See the image below for the relationship between degrees and brads and how the COS
function values relate to to the 127-unit circle.</p>
<center>
<img src="../../graphics/SIN_COS.png" border="0" alt="" />
</center>
<p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" />
</p><pre class="BScode" xml:space="preserve">
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
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_7" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />SQR &#160;&#160;(Returns Square Root of Value)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_7" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="square"></a>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).</p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
Main:
DEBUG SQR 100 ' Display 10
DEBUG SQR 99 ' Display 9 (due to truncation)
END
</pre>
</div>
</div>
<div class="MCDropDown">
<div class="MCDropDownHead_0"><a class="MCDropDownHotSpot_0" href="javascript:void(0);" id="MCDropDownHotSpot_1311391400_8" onclick="FMCDropDown( this ); return false;"><img style="border: none;margin-right: 2px;" src="../../SkinSupport/DropDownClosed.gif" MadCap:altsrc="../../SkinSupport/DropDownOpen.gif" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../SkinSupport/DropDownOpen.gif' ); }" class="MCDropDownIcon" />NOT &#160;&#160;(Logical NOT)</a>
</div>
<div class="MCDropDownBody" id="MCDropDownBody_1311391400_8" style="display: none;">
<p>&#160;</p>
<p class="PlainText"><a name="not"></a>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:</p>
<p class="PlainText"><tt>&#160; <span class="code_in_text">&#160; NOT F = T
&#160; &#160; NOT T = F</span></tt>
</p>
<p class="PlainText">The result returned by NOT will be True or False.</p>
<img src="../../graphics/bs2all_inline.gif" border="0" alt="" /><pre class="BScode" xml:space="preserve">
speed VAR Byte
Main:
DEBUG CLS, "Enter speed: "
DEBUGIN DEC speed
IF NOT (speed &lt; 65) THEN ' speed must be less than 65
DEBUG CR, "Sorry ... too fast!"
ELSE
DEBUG CR, "Enjoy your trip."
ENDIF
END
</pre>
</div>
</div>
<p>&#160;</p>
<div class="Col2">
<div class="MasterFoot">
<p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
</p>
</div>
</div>
<div class="Col2">
<div class="MasterFoot">
<p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
<p style="text-align: right;">Copyright ©&#160;<span class="ContactInfoCompanyName">Parallax Inc.</span></p>
<p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
</p>
</div>
</div>
<script type="text/javascript">/* <![CDATA[ */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-285614-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
/* ]]> */</script>
<script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
</script>
</body>
</html>