Re: How do you interpret this code ?: Modified := Original + 1;
- From: nic@xxxxxx
- Date: 14 Mar 2007 03:53:26 -0700
On 14 Mar., 01:12, "Skybuck Flying" <s...@xxxxxxxxxxx> wrote:
Hmm I compared Test1( Byte, Word ) and Test2( Word, Byte )
Same kind of code generated... except one is 8 bit and one 16 bit...
procedure Test1( const Original : byte; var Modified : word );
begin
Modified := Original + 1; // original 255, no problem.
end;
See this is actually where it gets interesting.
If I use Delphi to compile this function and call it with 255 as the
byte value, it returns 256 in the word.
But if I use my (16 bit) microprocessor Pascal compiler to do the same
- it returns 0 in the word.
At first I thought Delphi was wrong here so I spent a few minutes
reading through the Extended Pascal ISO document I had (rule 6.8.1
mentions operator precedence) but nowhere does it state if ":=" has or
has not precedence over other operators. In C, the assignment operator
"=" (equal to ":=" in Pascal) has the lowest precedence of all
operators except for things like "," thats not used in Pascal.
SO - a C compiler would translate this as
temp (byte) gets the value of original (byte) [255]
temp (byte) is increased by one [0]
modified (word) gets the value of temp (byte) [0]
My microprocessor compiler does the same. And this is what I would
expect from that function.
Delphi, though, translates this into
temp (word) gets the value of original (byte) [255]
temp (word) is increased by one [256]
modified (word) gets the value of temp (word) [256]
If assignments has to have any meaning, the ":=" operator must have
the lowest precedence. Play with paranthesis; the statement (a:=b)+1
has no meaning (equal to ":=" having precedence over "+"). But with
this assumption Delphi is wrong. The function must then return 0, not
256 as no 16 bit values exist before the assignment operator is used.
.
- Follow-Ups:
- Re: How do you interpret this code ?: Modified := Original + 1;
- From: Al Slater
- Re: How do you interpret this code ?: Modified := Original + 1;
- From: Rob Kennedy
- Re: How do you interpret this code ?: Modified := Original + 1;
- References:
- How do you interpret this code ?: Modified := Original + 1;
- From: Skybuck Flying
- Re: How do you interpret this code ?: Modified := Original + 1;
- From: Rob Kennedy
- Re: How do you interpret this code ?: Modified := Original + 1;
- From: Jim P
- Re: How do you interpret this code ?: Modified := Original + 1;
- From: Rob Kennedy
- Re: How do you interpret this code ?: Modified := Original + 1;
- From: Skybuck Flying
- Re: How do you interpret this code ?: Modified := Original + 1;
- From: Skybuck Flying
- How do you interpret this code ?: Modified := Original + 1;
- Prev by Date: Re: How do you interpret this code ?: Modified := Original + 1;
- Next by Date: Re: OvrInit Unknown identifier
- Previous by thread: Re: How do you interpret this code ?: Modified := Original + 1;
- Next by thread: Re: How do you interpret this code ?: Modified := Original + 1;
- Index(es):
Relevant Pages
|