Difference between revisions of "Word"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
(rewrite)
Line 1: Line 1:
 
{{Word}}
 
{{Word}}
  
 +
A ''word'' is the processor’s native data unit.
 +
Modern consumer processors have a word width of [[64 bit|64 bits]].
  
Back to [[Data type|data types]].
+
== Data type ==
 +
Most [[RTL|run-time libraries]] provide the native data type of a processor as the Pascal data type <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>.
 +
It is a subset of all whole numbers (non-negative integers) that can be represented by the processor’s natural data unit size.
  
 +
In [[GNU Pascal]] a <syntaxhighlight lang="pascal" inline>word</syntaxhighlight> is just an alias for [[Cardinal|<syntaxhighlight lang="pascal" inline>cardinal</syntaxhighlight>]], which has the same properties regarding possible values.
  
A Word is a 16 bits unsigned [[Integer|integer]] value. It requires two bytes of memory space. Its maximal value is 65,535 ((2^16 - 1) or [[Hexadecimal|hexadecimal]] [[Dollar_sign|$]]FFFF (or 0xFFFF) and its minimal value is 0.
+
On a 64-bit architecture this means a <syntaxhighlight lang="pascal" inline>word</syntaxhighlight> is an integer within the range <math>[0,~2^{64}-1]</math>.
  
Nowadays Words are rarely used in arithmetic operations (because most of the CPU have some [[32 bit]] registers, if not [[64 bit]]), but they can be encountered in some API to encode some flags with logical operations (like in a [[Set|set]]).
+
=== FPC ===
And some [[Embedded|embedded]] systems.
+
Contrary to the data type’s name, in [[FPC]] a word is defined as a subrange data type <syntaxhighlight lang="pascal" inline>0..65535</syntaxhighlight>.
 +
<syntaxhighlight lang="pascal" inline>65535</syntaxhighlight> is <math>2^{16}-1</math>.
 +
Thus a {{Doc|package=RTL|unit=system|identifier=word|text=<syntaxhighlight lang="pascal" inline>system.word</syntaxhighlight>}} occupies two bytes of space.
 +
Subrange data types are stored in a quantity that serves best the goals of performance and memory efficiency.
  
A simple program using a word [[Type|type]] [[Variable|variables]]:
+
This deviation from the normal usage of the term ''word'' in computer science cannot be explained.<!-- Explain it, if you know why. I don't wanna spread rumors. -->
 +
Instead, one has to use the data type {{Doc|package=RTL|unit=system|identifier=ptruint|text=<syntaxhighlight lang="pascal" inline>system.ptrUInt</syntaxhighlight>}} if the processor’s native size is desired.
 +
This data type has the same properties as a word according to the normal definition, but is more cryptic and unreadable (try pronouncing ptruint).
  
<syntaxhighlight lang=pascal>
+
In FPC a [[Smallint|<syntaxhighlight lang="pascal" inline>smallInt</syntaxhighlight>]] has the same size as a <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>.
 
 
program wordexam;
 
 
 
var
 
  w1,w2: word;
 
 
 
begin
 
  w1 := 1;
 
  w2 := $2ef;
 
  writeln ( w1,'+', w2,' = ' , w1+w2);
 
end.
 
 
 
</syntaxhighlight>
 
  
 
{{Data types}}
 
{{Data types}}

Revision as of 14:38, 21 May 2020

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

A word is the processor’s native data unit. Modern consumer processors have a word width of 64 bits.

Data type

Most run-time libraries provide the native data type of a processor as the Pascal data type word. It is a subset of all whole numbers (non-negative integers) that can be represented by the processor’s natural data unit size.

In GNU Pascal a word is just an alias for cardinal, which has the same properties regarding possible values.

On a 64-bit architecture this means a word is an integer within the range [math]\displaystyle{ [0,~2^{64}-1] }[/math].

FPC

Contrary to the data type’s name, in FPC a word is defined as a subrange data type 0..65535. 65535 is [math]\displaystyle{ 2^{16}-1 }[/math]. Thus a system.word occupies two bytes of space. Subrange data types are stored in a quantity that serves best the goals of performance and memory efficiency.

This deviation from the normal usage of the term word in computer science cannot be explained. Instead, one has to use the data type system.ptrUInt if the processor’s native size is desired. This data type has the same properties as a word according to the normal definition, but is more cryptic and unreadable (try pronouncing ptruint).

In FPC a smallInt has the same size as a word.


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring