Lo

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Lo

Carsten Bager
Hi
Is there a reason why the lo function returns the lo word from a longint in FPC and not the lo
byte as in Delphi.

Regards Carsten




------


Delphi 5 Doc
------------------------------
function Lo(X): Byte;

Description

Lo returns the low-order Byte of the argument X as an unsigned value. X is an expression of
type Integer.


Fpc 224 Doc
------------------
Synopsis: Return low byte/word of value.
Declaration: function lo(B: Byte) : Byte
function lo(i: Integer) : Byte
function lo(w: Word) : Byte
function lo(l: LongInt) : Word
function lo(l: DWord) : Word
function lo(i: Int64) : DWord
function lo(q: QWord) : DWord
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Lo

Jonas Maebe-2

On 11 Jan 2011, at 10:38, Carsten Bager wrote:

> Is there a reason why the lo function returns the lo word from a  
> longint in FPC and not the lo
> byte as in Delphi.

It was decided/implemented before Delphi existed and when FPC's main  
goal was still to become a 32 bit alternative to Turbo Pascal. Maximal  
Borland compatibility was not as high on the list at the time as  
trying to "fix" the language where we believed Borland made a bad  
decision. Getting the lower word seemed (and still seems, to me at  
least) more logical and useful for longints than returning the lower  
byte, hence the decision.

Furthermore, lo() is implemented as an inline function in the system  
unit, which means that its result cannot differ depending on the  
language mode of the current source file. It could be changed into an  
internal compiler intrinsic so the behaviour differs for different  
syntax modes, but nobody has ever cared enough about this difference  
to make that change.

This and other similar differences are also documented at http://www.freepascal.org/port.var


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Lo

_-jane-_
In reply to this post by Carsten Bager
A simple but fully functional kludge to make lo compatible with Delphi is

 type lo=byte;

Unfortunately for the similar cases of hi and swap you must provide a function which cannot be used in a manifest constant expression.
___________________________________________________________
WEB.DE DSL Doppel-Flat ab 19,99 €/mtl.! Jetzt mit
gratis Handy-Flat! http://produkte.web.de/go/DSL_Doppel_Flatrate/2
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Lo

Tomas Hajny-2
On Thu, January 13, 2011 09:01, [hidden email] wrote:
> A simple but fully functional kludge to make lo compatible with Delphi is
>
>  type lo=byte;
>
> Unfortunately for the similar cases of hi and swap you must provide a
> function which cannot be used in a manifest constant expression.

Well, you should probably mention that this replacement solution is not
portable to big endian platforms / targets.

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Lo

Carsten Bager
 
> Well, you should probably mention that this replacement solution is not
> portable to big endian platforms / targets.
>
> Tomas

If one want to write portable code, what would be best, or would both work on a big endian
platform.

Carsten

var
  b:byte;
  i:LongInt;

b := i and 255;
b:= i mod 256;
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Lo

Jonas Maebe-2
In reply to this post by Tomas Hajny-2

On 13 Jan 2011, at 09:32, Tomas Hajny wrote:

> On Thu, January 13, 2011 09:01, [hidden email] wrote:
>> A simple but fully functional kludge to make lo compatible with  
>> Delphi is
>>
>> type lo=byte;
>>
>> Unfortunately for the similar cases of hi and swap you must provide a
>> function which cannot be used in a manifest constant expression.
>
> Well, you should probably mention that this replacement solution is  
> not
> portable to big endian platforms / targets.

It is portable to big endian targets. byte(longintvar) will return the  
least significant byte on all targets (otherwise there would be almost  
no portability of any integer code between big and little endian  
targets).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Lo

Tomas Hajny-2
On Thu, January 13, 2011 11:53, Jonas Maebe wrote:

>
> On 13 Jan 2011, at 09:32, Tomas Hajny wrote:
>
>> On Thu, January 13, 2011 09:01, [hidden email] wrote:
>>> A simple but fully functional kludge to make lo compatible with
>>> Delphi is
>>>
>>> type lo=byte;
>>>
>>> Unfortunately for the similar cases of hi and swap you must provide a
>>> function which cannot be used in a manifest constant expression.
>>
>> Well, you should probably mention that this replacement solution is
>> not
>> portable to big endian platforms / targets.
>
> It is portable to big endian targets. byte(longintvar) will return the
> least significant byte on all targets (otherwise there would be almost
> no portability of any integer code between big and little endian
> targets).

OK, sorry for confusion then.

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal