uInteger

New to FreeBASIC? Post your questions here.
Post Reply
HillbillyGeek
Posts: 50
Joined: Oct 27, 2011 1:51
Location: Texas
Contact:

uInteger

Post by HillbillyGeek »

Saw this in a code snippet the other day (following Dim, of course)......what's the 'U' about???
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: uInteger

Post by Josep Roca »

Unsigned.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: uInteger

Post by counting_pine »

Short explanation:
Literally, "UInteger" means "Unsigned Integer". In fact you can write it either way in code.
An Unsigned Integer cannot be negative (in other words, it can't have a negative sign), which is why it's called Unsigned. But it can hold twice as many positive values as a normal (signed) Integer.

A normal Integer (in 32-bit mode) can hold values between around -2 billion and +2 billion.
An Unsigned Integer can hold values between 0 and around 4 billion.


Long explanation:
The built-in number types in FreeBASIC each have a fixed size:
Byte: 1 byte (8 bits)
Short: 2 bytes (16 bits)
Long: 4 bytes (32 bits)
Longint: 8 bytes (64 bits)
(Integer is either the same size as Long or Longint, depending on whether you compile 32-bit or 64-bit code.)

The smaller a type is, the smaller the range of possible values it can hold.
For example, a Byte can hold 256 different values (because there are 2^8 = 256 possible values you can make with 8 bits).

Each type can either be Unsigned or Signed.
- "Unsigned" means it can only hold non-negative values (i.e. positive or 0).
- "Signed" means it can hold non-negative and negative values

Unsigned is the simpler case. It can only hold positive or zero values.
For example, an Unsigned Byte can hold the first 256 values starting at zero, meaning it can hold any value between 0 and 255 (inclusive).

If a type is Signed, it can hold negative values. But it uses up one of its bits to denote whether the number is negative or not.
For example, a Signed Byte can hold 128 different non-negative values (0 to 127), and 128 different negative values (-128 to -1).
(Because there are 2^7 = 128 different possible values you can make with 7 bits, and the remaining 1 bit tells us whether the number is negative or not.)

So a normal (signed) Byte can hold values from -128 to 127, and an Unsigned Byte can hold values from 0 to 255.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: uInteger

Post by Tourist Trap »

HillbillyGeek wrote:Saw this in a code snippet the other day (following Dim, of course)......what's the 'U' about???
There is a help page for Unsigned with a short word of explaination. But this is true that this is not mentionned there that U stands for Unsigned so you have to relate the facts yourself. Unsigned Integer is anyway legal syntax and equivalent to uInteger.

Whatever, the numbers that in freebasic mimic natural integers are all limited to a given range. The best way to visualize this range is to write it as:
-2^(R - 1) to 2^(R - 1) - 1 for signed version
0 to 2^R - 1 for unsigned version

Suppose R =2 (which doesn't exist in FB but is simpler for demonstration), then your number if signed will be comprised between: -2^(2 - 1) and 2^(2 - 1) - 1, or:
-2, -1, 0, +1

The unsigned version of this would stands between, 0 and 2^2 - 1:
0, +1, +2, +3

Note that if you reach the max value and do a +1 operation you return back to min value and so on. So all of this is not like natural integers of the real life in this sense.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: uInteger

Post by BasicCoder2 »

counting_pine wrote:The built-in number types in FreeBASIC each have a fixed size:
And to understand the why a particular range of values is to enter the world of binary numbers.
https://www.mathsisfun.com/binary-number-system.html

.
Last edited by BasicCoder2 on Jun 04, 2017 13:44, edited 1 time in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: uInteger

Post by counting_pine »

I've updated the wiki now so that UInteger is described as equivalent to Unsigned Integer. (And similarly with other Unsigned types.)
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: uInteger

Post by integer »

counting_pine wrote:I've updated the wiki now so that UInteger is described as equivalent to Unsigned Integer. (And similarly with other Unsigned types.)
Thank you!
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: uInteger

Post by jevans4949 »

@HillbillyGeek: Also note that if you set up a loop in the form:

Code: Select all

Dim u as ubyte
For u = 0 to 255
'Whatwever
Next u
... the loop will never end. At the end of each iteration, 1 is added to u, and after 255 it rolls over to 0, and starts all over again.

There are ways round this. Most obvious is to put a line in before the "Next" statement to say "If u=255 then exit for"

Just put this in as new programmers often report this as a bug, and oldies spend time telling them it's not.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: uInteger

Post by Tourist Trap »

counting_pine wrote:I've updated the wiki now so that UInteger is described as equivalent to Unsigned Integer. (And similarly with other Unsigned types.)
Thanks.
jevans4949 wrote: Just put this in as new programmers often report this as a bug, and oldies spend time telling them it's not.
Ah yes, I reported this one for sure...
RobertLM78
Posts: 4
Joined: May 04, 2015 2:54

Re: uInteger

Post by RobertLM78 »

jevans4949 wrote:@HillbillyGeek: Also note that if you set up a loop in the form:

Code: Select all

Dim u as ubyte
For u = 0 to 255
'Whatwever
Next u
... the loop will never end. At the end of each iteration, 1 is added to u, and after 255 it rolls over to 0, and starts all over again.

There are ways round this. Most obvious is to put a line in before the "Next" statement to say "If u=255 then exit for"

Just put this in as new programmers often report this as a bug, and oldies spend time telling them it's not.
xD
I see why that would be - I haven't come across it yet, but it's good to know! (One less 'bug' report ;P )
Post Reply