Difference between revisions of "UNSIGNED"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Cyperium
Line 1: Line 1:
'''_UNSIGNED''' defines a numerical value as being positive using '''QB64''' only.
{{KW|_UNSIGNED}} defines a numerical value as being positive using '''QB64''' only.




:::::''Syntax:'' [[_DEFINE]] LetterRange AS [_UNSIGNED] datatype
{{PageSyntax}}
:<code>{{KW|_DEFINE}} {{Parameter|LetterRange}} {{KW|AS}} [{{KW|_UNSIGNED}}] {{Parameter|datatype}}</code>




* Although _UNSIGNED values can use negative values the result subtracts from the highest value of the number's type keeping it positive.
{{PageDescription}}
* Datatype can be any of the following: [[INTEGER]], [[SINGLE]], [[DOUBLE]], [[LONG]], [[STRING]], [[_BIT]], [[_BYTE]], [[_INTEGER64]], [[_FLOAT]]
* Although {{KW|_UNSIGNED}} values can use negative values the result subtracts from the highest value of the number's type keeping it positive.
* _UNSIGNED can be used in a [[_DEFINE]] statement to set undefined variable name first letters as all positive only values.
* Datatype can be any of the following: {{KW|INTEGER}}, {{KW|SINGLE}}, {{KW|DOUBLE}}, {{KW|LONG}}, {{KW|STRING}}, {{KW|_BIT}}, {{KW|_BYTE}}, {{KW|_INTEGER64}}, {{KW|_FLOAT}}
* Can also be used in [[DIM]] statements or Subprocedure parameter definitions following [[AS]].
* {{KW|_UNSIGNED}} can be used in a {{KW|_DEFINE}} statement to set undefined variable name first letters as all positive only values.
* _UNSIGNED allows larger positive numerical variable value limits than signed ones.
* Can also be used in {{KW|DIM}} statements or Subprocedure parameter definitions following {{KW|AS}}.
* {{KW|_UNSIGNED}} allows larger positive numerical variable value limits than signed ones.
* The Unsigned variable type suffix used is the '''tilde ~''' before the number's own type suffix: variablename~&
* The Unsigned variable type suffix used is the '''tilde ~''' before the number's own type suffix: variablename~&




''Example 1:'' How negative values affect the _UNSIGNED value returned by a [[_BYTE]](8 bits).
{{PageExamples}}
''Example 1:'' How negative values affect the {{KW|_UNSIGNED}} value returned by a {{KW|_BYTE}}(8 bits).


::::00000001 - unsigned & signed are both 1
::::00000001 - unsigned & signed are both 1
Line 22: Line 25:


''Example 2:'' Demonstrating how _UNSIGNED variables expand the Integer range.
''Example 2:'' Demonstrating how _UNSIGNED variables expand the Integer range.
{{CodeStart}}


:DIM n AS _UNSIGNED INTEGER
{{Cl|DIM}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}
:DIM pn AS _UNSIGNED INTEGER
{{Cl|DIM}} pn {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}
:LOCATE 3, 6: PRINT "Press Esc to exit loop"
{{Cl|LOCATE}} 3, 6: {{Cl|PRINT}} "Press Esc to exit loop"
:FOR n = 1 TO 80000
{{Cl|FOR...NEXT|FOR}} n = 1 {{Cl|TO}} 80000
::_LIMIT 10000 ' 6.5 second loop  
  {{Cl|_LIMIT}} 10000 ' 6.5 second loop  
::LOCATE 12, 37: PRINT n ' display current value
  {{Cl|LOCATE}} 12, 37: {{Cl|PRINT}} n ' display current value
::IF n > 0 THEN pn = n ' find highest value
  {{Cl|IF...THEN|IF}} n > 0 {{Cl|THEN}} pn = n ' find highest value
::IF n = 0 THEN Count = Count + 1: LOCATE 14, 37: PRINT "Count:"; Count; "Max:"; pn
  {{Cl|IF...THEN|IF}} n = 0 {{Cl|THEN}} Count = Count + 1: {{Cl|LOCATE}} 14, 37: {{Cl|PRINT}} "Count:"; Count; "Max:"; pn
::IF INP(&H60) = 1 THEN EXIT FOR ' escape key exit
  {{Cl|IF...THEN|IF}} {{Cl|INP}}(&H60) = 1 {{Cl|THEN}} {{Cl|EXIT|EXIT FOR}} ' escape key exit
:NEXT n
{{Cl|NEXT}} n
:END
{{Cl|END}}


{{CodeEnd}}
{{OutputStart}}


''Explanation:'' The maximum value can only be 65535(32767 + 32768) so the FOR loop repeats itself. Remove the _UNSIGNED parts and run it again.
  Press Esc to exit loop






''See also:'' [[DECLARE]], [[SUB]], [[FUNCTION]], [[DIM]], [[_DEFINE]], [[DEFSTR]], [[DEFLNG]], [[DEFINT]], [[DEFSNG]], [[DEFDBL]], [[ABS]], [[SGN]]


                          65462


                          Count: 13 Max: 65535


==Navigation:==


[[Keyword_Reference_-_Alphabetical|Go to Keyword Reference - Alphabetical]]
{{OutputEnd}}
''Explanation:'' The maximum value can only be 65535(32767 + 32768) so the FOR loop repeats itself. Remove the {{KW|_UNSIGNED}} parts and run it again.


[[Keyword Reference - By usage|Go to Keyword Reference - By usage]]
 
 
{{PageSeeAlso}}
*{{KW|DECLARE}}, {{KW|SUB}}, {{KW|FUNCTION}}
*{{KW|DIM}}, {{KW|_DEFINE}}, {{KW|DEFSTR}}, {{KW|DEFLNG}}, {{KW|DEFINT}}, {{KW|DEFSNG}}, {{KW|DEFDBL}}
*{{KW|ABS}}, {{KW|SGN}}
 
 
 
{{PageNavigation}}

Revision as of 01:29, 27 February 2010

_UNSIGNED defines a numerical value as being positive using QB64 only.


Syntax

_DEFINE LetterRange AS [_UNSIGNED] datatype


Description

  • Although _UNSIGNED values can use negative values the result subtracts from the highest value of the number's type keeping it positive.
  • Datatype can be any of the following: INTEGER, SINGLE, DOUBLE, LONG, STRING, _BIT, _BYTE, _INTEGER64, _FLOAT
  • _UNSIGNED can be used in a _DEFINE statement to set undefined variable name first letters as all positive only values.
  • Can also be used in DIM statements or Subprocedure parameter definitions following AS.
  • _UNSIGNED allows larger positive numerical variable value limits than signed ones.
  • The Unsigned variable type suffix used is the tilde ~ before the number's own type suffix: variablename~&


Examples

Example 1: How negative values affect the _UNSIGNED value returned by a _BYTE(8 bits).

00000001 - unsigned & signed are both 1
01111111 - unsigned & signed are both 127
11111111 - unsigned is 255 but signed is -1
11111110 - unsigned is 254 but signed is -2
11111101 - unsigned is 253 but signed is -3

Example 2: Demonstrating how _UNSIGNED variables expand the Integer range.

DIM n AS _UNSIGNED INTEGER DIM pn AS _UNSIGNED INTEGER LOCATE 3, 6: PRINT "Press Esc to exit loop" FOR n = 1 TO 80000 _LIMIT 10000 ' 6.5 second loop LOCATE 12, 37: PRINT n ' display current value IF n > 0 THEN pn = n ' find highest value IF n = 0 THEN Count = Count + 1: LOCATE 14, 37: PRINT "Count:"; Count; "Max:"; pn IF INP(&H60) = 1 THEN EXIT FOR ' escape key exit NEXT n END

Press Esc to exit loop 65462 Count: 13 Max: 65535

Explanation: The maximum value can only be 65535(32767 + 32768) so the FOR loop repeats itself. Remove the _UNSIGNED parts and run it again.


See also




Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page