UNSIGNED: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_UNSIGNED}} _UNSIGNED defines a numerical value as being only positive. {{PageSyntax}} : DIM {{Parameter|variable}} AS [{{KW|_UNSIGNED}}] {{Parameter|datatype}} : _DEFINE {{Parameter|letterRange}} AS [{{KW|_UNSIGNED}}] {{Parameter|datatype}} {{PageDescription}} * Datatype can be any of the following: INTEGER, LONG, _BIT, _BYTE, _INTEGER64, _OFFSET *'''SINGLE, DOUBLE and _FLOAT variable types can...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 3: Line 3:




{{PageSyntax}}  
{{PageSyntax}}
: [[DIM]] {{Parameter|variable}} [[AS]] [{{KW|_UNSIGNED}}] {{Parameter|datatype}}
: [[DIM]] {{Parameter|variable}} [[AS]] [[[_UNSIGNED]]] {{Parameter|datatype}}


: [[_DEFINE]] {{Parameter|letterRange}} [[AS]] [{{KW|_UNSIGNED}}] {{Parameter|datatype}}
: [[_DEFINE]] {{Parameter|letterRange}} [[AS]] [[[_UNSIGNED]]] {{Parameter|datatype}}




Line 18: Line 18:




<center>How negative values affect the [[_UNSIGNED]] value returned by a [[_BYTE]] (8 bits). </center>
<center>How negative values affect the [[_UNSIGNED]] value returned by a [[_BYTE]] (8 bits).</center>
{{WhiteStart}}
{{FixedStart}}
                         00000001 - unsigned & signed are both 1  
                         00000001 - unsigned & signed are both 1
                         01111111 - unsigned & signed are both 127
                         01111111 - unsigned & signed are both 127
                         11111111 - unsigned is 255 but signed is -1
                         11111111 - unsigned is 255 but signed is -1
                         11111110 - unsigned is 254 but signed is -2
                         11111110 - unsigned is 254 but signed is -2
                         11111101 - unsigned is 253 but signed is -3
                         11111101 - unsigned is 253 but signed is -3
{{WhiteEnd}}
{{FixedEnd}}




{{PageExamples}}
{{PageExamples}}
''Example 1:''  In '''QB64''', when a signed [[INTEGER]] value exceeds 32767, the value may become a negative value:
''Example 1:''  In '''QB64''', when a signed [[INTEGER]] value exceeds 32767, the value may become a negative value:
{{CodeStart}} '' ''
{{CodeStart}}
i% = 38000
i% = 38000
{{Cl|PRINT}} i% '' ''
{{Cl|PRINT}} i%
{{CodeEnd}}{{OutputStart}}-27536
{{CodeEnd}}{{OutputStart}}-27536
{{OutputEnd}}
{{OutputEnd}}
Line 39: Line 39:


''Example 2:'' In '''QB64''', [[_UNSIGNED]] [[INTEGER]] values greater than 65535 cycle over again from zero:
''Example 2:'' In '''QB64''', [[_UNSIGNED]] [[INTEGER]] values greater than 65535 cycle over again from zero:
{{CodeStart}} '' ''
{{CodeStart}}
i~% = 70000
i~% = 70000
{{Cl|PRINT}} i~% '' ''
{{Cl|PRINT}} i~%
{{CodeEnd}}{{OutputStart}} 4464
{{CodeEnd}}{{OutputStart}} 4464
{{OutputEnd}}
{{OutputEnd}}
:''Explanation:'' In QB64 an unsigned integer value of 65536 would be 0 with values increasing by the value minus 65536.  
:''Explanation:'' In QB64 an unsigned integer value of 65536 would be 0 with values increasing by the value minus 65536.




''Example 3:'' Demonstrating how _UNSIGNED variables expand the [[INTEGER]] range.
''Example 3:'' Demonstrating how _UNSIGNED variables expand the [[INTEGER]] range.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DIM}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}
{{Cl|DIM}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}
{{Cl|DIM}} pn {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}
{{Cl|DIM}} pn {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}
{{Cl|LOCATE}} 3, 6: {{Cl|PRINT}} "Press Esc to exit loop"
{{Cl|LOCATE}} 3, 6: {{Cl|PRINT}} "Press Esc to exit loop"
{{Cl|FOR...NEXT|FOR}} n = 1 {{Cl|TO}} 80000
{{Cl|FOR...NEXT|FOR}} n = 1 {{Cl|TO}} 80000
   {{Cl|_LIMIT}} 10000 ' 6.5 second loop  
   {{Cl|_LIMIT}} 10000 ' 6.5 second loop
   {{Cl|LOCATE}} 12, 37: {{Cl|PRINT}} n ' display current value
   {{Cl|LOCATE}} 12, 37: {{Cl|PRINT}} n ' display current value
   {{Cl|IF...THEN|IF}} n > 0 {{Cl|THEN}} pn = n ' find highest value
   {{Cl|IF...THEN|IF}} n > 0 {{Cl|THEN}} pn = n ' find highest value
Line 59: Line 59:
   {{Cl|IF...THEN|IF}} {{Cl|INP}}(&H60) = 1 {{Cl|THEN}} {{Cl|EXIT|EXIT FOR}} ' escape key exit
   {{Cl|IF...THEN|IF}} {{Cl|INP}}(&H60) = 1 {{Cl|THEN}} {{Cl|EXIT|EXIT FOR}} ' escape key exit
{{Cl|NEXT}} n
{{Cl|NEXT}} n
{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 75: Line 75:
{{OutputEnd}}
{{OutputEnd}}
''Explanation:'' The maximum value can only be 65535 (32767 + 32768) so the FOR loop repeats itself. Remove the [[_UNSIGNED]] parts and run it again.
''Explanation:'' The maximum value can only be 65535 (32767 + 32768) so the FOR loop repeats itself. Remove the [[_UNSIGNED]] parts and run it again.





Latest revision as of 01:18, 29 January 2023

_UNSIGNED defines a numerical value as being only positive.


Syntax

DIM variable AS [[[_UNSIGNED]]] datatype
_DEFINE letterRange AS [[[_UNSIGNED]]] datatype


Description

  • Datatype can be any of the following: INTEGER, LONG, _BIT, _BYTE, _INTEGER64, _OFFSET
  • SINGLE, DOUBLE and _FLOAT variable types cannot be _UNSIGNED.
  • _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 (~), right before the number's own type suffix: variableName~&


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


Examples

Example 1: In QB64, when a signed INTEGER value exceeds 32767, the value may become a negative value:

i% = 38000
PRINT i%
-27536
Explanation: Use an _UNSIGNED INTEGER or a ~% variable type suffix for only positive integer values up to 65535.


Example 2: In QB64, _UNSIGNED INTEGER values greater than 65535 cycle over again from zero:

i~% = 70000
PRINT i~%
 4464
Explanation: In QB64 an unsigned integer value of 65536 would be 0 with values increasing by the value minus 65536.


Example 3: 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:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link