Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-01

Lo

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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
Jonas Maebe
to Carsten Bager
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

Unknown author

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

Tomas Hajny
to Unknown author
Show quoted text 6 lines
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

Carsten Bager
to Tomas Hajny
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;
Jonas Maebe
to Tomas Hajny: Well, you should probably mention that this replacement solution is not portable to big…
Tomas Hajny wrote 12 lines
On Thu, January 13, 2011 09:01, 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

Tomas Hajny
to Jonas Maebe
Jonas Maebe wrote 20 lines
On 13 Jan 2011, at 09:32, Tomas Hajny wrote: > On Thu, January 13, 2011 09:01, 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