PDA

View Full Version : Adding over boundaries



User137
14-05-2008, 12:57 PM
Is it ok to use computer math in delphi to loop numbers through their boundaries?

Example:

var i: integer; b: byte;
begin
b:=0;
for i:=0 to 255 do
b:=b+1;
// now i should be 100% sure that b = 0, right?
end;

And same logic when decreasing, no matter what type it is. From my test it does work like that for byte, shortint and word but can i trust it to work on every PC and operating system, and free pascal?

Brainer
14-05-2008, 01:25 PM
You can try this:

var
I: Integer;
B: Byte;
begin
B := 0;
for I := 0 to High(Byte) do
B := B + 1;


But for all I know, Byte is 0.255, so if you assign a too big number, it'll be overrided - if you assign "512", B = "255". The same is with other ordinal types, but I guess they all have negative values - fx. Shortint is -128..127. So if you assign "312" to a ShortInt variable, it'll be set to "57".

I hope it's clear.

User137
14-05-2008, 02:10 PM
You can try this:
But for all I know, Byte is 0.255, so if you assign a too big number, it'll be overrided - if you assign "512", B = "255". The same is with other ordinal types, but I guess they all have negative values - fx. Shortint is -128..127. So if you assign "312" to a ShortInt variable, it'll be set to "57".
Hehe, small fixes for you after testing:
if you assign "512", B = "0"
if you assign "312" to a ShortInt variable, it'll be set to "56"

But hardware/OS side?

waran
14-05-2008, 02:15 PM
Generic datatypes behave the same on all platforms (regardless endianess).
However if you want to select byte-compontents then byte order matters.

As for sizes, Integer is dynamic, signed and Cardinal is dynamic, unsiged.
The other types have fixed lenghts.

User137
14-05-2008, 02:55 PM
What do you mean dynamic, isn't integer and cardinal always 32bit?

waran
15-05-2008, 04:33 AM
Cardinal and Integer adjust to the systems native size.
On a 64 bit-sys Cardianal and Integer have 64 bit.

Brainer
15-05-2008, 06:06 PM
Hehe, small fixes for you after testing:
if you assign "512", B = "0"
if you assign "312" to a ShortInt variable, it'll be set to "56"

But hardware/OS side?
Sorry, my bad. :)

And yes, 100% agree with waran.

User137
15-05-2008, 11:22 PM
Well it's weird, what type would we use then to access 32 bit integer on a 64bit system? Longint and Longword should be equivalents to integer and cardinal. And while finding any information out of this subject with google seems impossible, i would also like to know in which point the integer would be considered as 64 bit, compiled under 64 bit OS or while running on 64 bit OS?

This site also says http://qc.borland.com/wc/qcmain.aspx?d=53174
"In a future 64-bit version of Delphi, however, that will no longer work: Pointers will be extended to 64 bits, while Cardinal and Integer will remain as 32-bit types."

Though that post got 0/5 rating out of 12 votes, whatever the reason :P

waran
16-05-2008, 07:40 AM
No. LongWord and LongInt always have 32 bits (one of the nicest
features of the pascal language compared to C is that you can actually
trust your datatypes - "they" call it 'porting problems' of course -.-).

The size of the Integer becomes 64 bit if you compile your code to a
64 bit binary (then it won't run under a 32 bit os).

If you compile under/for 32 bit it will run both in Win64 (on an emulation
layer) and Win32 - with LongInt sized Integer (on both systems!).

Referring to the post at gc:
Its actually not a strict rule that Cardinal/Integer matches the native size.
It was just like that all the years before (16, 32 bit TP/Delphi) and in 64 bit
freepascal. Maybe Borland/CodeGear/Whatever is going to break it -
but like I said: It just would'n fit and bring Delphi one step closer to C
(more weirdnesses).

marcov
23-05-2008, 10:45 AM
Cardinal and Integer adjust to the systems native size.
On a 64 bit-sys Cardianal and Integer have 64 bit.

No, ptrint and ptruint scale with ptrsize. cardinal and integer are 32-bit.

See also http://www.stack.nl/~marcov/buildfaq/buildfaq.html#x1-660005.1

and no, also in C often integer is not 64-bit on 64-bit systems.