Learn Object Pascal
Part 4 - Operators
Arithmetic Operators:These operators need two operands.
+ Addition. Operands : real or integer Result : real or integer
- Subtraction. Operands : real or integer Result : real or integer
* Multiplication. Operands : real or integer Result : real or integer
/ Real division. Operands : real or integer Result : real
div Integer division. Operands : integer Result : integer
div performs integer division, discarding the remainder
mod Modulus (remainder division). Operands : integer Result : integer
mod gives the remainder from dividing the division.
Do not attempt to use two operators side by side. Use instead parentheses like this : 1+1+(-3)
//Arithmetic operators
Memo1.Lines.Add(FloatToStr(5+2.5)); //: +
Memo1.Lines.Add(FloatToStr(6-2.2)); //: -
Memo1.Lines.Add(FloatToStr(6*1.5)); //: *
Memo1.Lines.Add(FloatToStr(6/1.6)); //: /
Memo1.Lines.Add(IntToStr(6 div 4));
//div performs integer division, discarding the remainder
Memo1.Lines.Add(IntToStr(6 mod 4));
//The mod gives the remainder from the division.
//parenthesis
Memo1.Lines.Add( IntToStr(1+1+(-3)) );
String operators: + :concatenate strings
//String operators
Memo1.Lines.Add('Hello' + 'World');
Memo1.Lines.Add('Hello' + 'World' + 'Again !');
More string functions can be found here
Set operations:
A set groups many values from an enumeration into an Ordinal type.
Ordinal types (ex : integer) are countable and ordered.
It is possible to start counting them one by one, in a specified order.
This property allows the operation of functions as Inc, Ord, Dec, Pred and Succ on ordinal types to be defined.
+ Union
- Difference
* Intersection
= Identical
<> Non identical
>=Is superset
<=Is subset
More about sets we will see in a next article. So right now we won't see an example.
Relational Operators:
These are two operaand operators
= Equal
<> Not equal
> Stricty less than
< Strictly greater than
>= Less than or equal
<= Greater than or equal
in Element of
More about these operators we will see in the next articles (if then else).
Boolean operators:
Are two operand logic operators
and Logic and
or Logic or
not Logic not
More about these operators we will see in the next articles (if then else).
Bitwise Operators:
Are used in order to modify a bit or bits of any given byte. Do not confuse the boolean operators and, or, and not with the bitwise operators and, or, and not.
Bitwise AND needs two operands like this :
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
One of the most common uses of bitwise and is to select a particular bit (or bits) from an integer value.
This technique is called masking.
Notice :
0 AND X = 0
1 AND X = X
Let's see a simple masking technique.
We have a single byte XXXXXXXX from this byte we want to read only the 3rd and forth bit from the beginning. We can use the bitwise operator and for masking like this :
XXXXXXXX AND 00110000 = 00xx0000
Bitwise OR needs two operands :
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1
Notice :
0 OR X = X
1 OR X = 1
Bitwise or is used to set high selected bits of a byte.
Lets see a use of the bitwise or.
We want to make sure that the bits 3 and 4 are high (1) and the others will remain unaltered.
XXXXXXXX OR 00110000 = XX11XXXX
Bitwise NOT operand is applied to a single operand.
not 0 = 1
not 1 = 0
Example : NOT 11101000 = 00010111
Bitwise XOR
0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0
Notice X XOR X = 0
X XOR Y = 1 where X<>Y
X XOR 0 = X
X XOR 1 = NOT X
xor is used to toggle selected bits from a byte (change from 0 to 1, or 1 to 0).
Let's say again that we want to toggle only the third and forth bit of a byte:
XXXXXXXX XOR 00110000 = XX(NOT X)(NOT X)XXXXX
Left bit-shift (shl)
The left shift operator shifts the bits to x positions to the left filling the last positions with x zeros .
Example : 11101000 shl 3 = 01000000
Another example : XXXXXXXX shl 4 = XXXX0000
Notice that if we do not get an overflow then x shl y = x * 2^y
Example :
We assume that x,y are bytes.
A byte has a max value of 255 values ( 2^(8*1) - 1)
if x = 10 and y = 3 then 10*(2^3) = 80 < 255 we do not get overflow
So it is safe to say in this case that :
10 shl 3 = 10 * 2^3
Infact : 10 = %00001010
%00001010 shl 3 = %01010000 = 80
if x = 100 and y = 3 then 100*(2^3) = 800 < 255 we get overflow
100 = %01100100
%01100100 shl 3 = %00100000 = 32
If x,y are integers :
max value = ( 2^(8*2) - 1) = 65535
So if x = 100 and y = 3 then 100*(2^3) = 800 < 65535 we do not get overflow
100 = %00000000 01100100
%00000000 01100100 shl 3 = %00000011 00100000 = 800 so we see that x shl y = x * 2^y is again valid
Right bit-shift (shr)
The right shift operator shifts the bits to x positions to the right filling the first positions with x zeros
Example : 11101000 shr 3 = 00011101
Another example : XXXXXXXX shr 4 = 0000XXXX
Notice that :
x shr y = x / 2^y
infact x = %11101000 = 232
and 232 shr 3 = %00011101 = 29 = 232/2^3
Below is a full example of what we have seen in this tutorial.
procedure TForm1.Button1Click(Sender: TObject);
var abyte, bbyte : byte; //One byte has 8 bits
ainteger, binteger : integer;
begin
//Arithmetic operators
Memo1.Lines.Add(FloatToStr(5+2.5)); //: +
Memo1.Lines.Add(FloatToStr(6-2.2)); //: -
Memo1.Lines.Add(FloatToStr(6*1.5)); //: *
Memo1.Lines.Add(FloatToStr(6/1.6)); //: /
Memo1.Lines.Add(IntToStr(6 div 4));
//div performs integer division, discarding the remainder
Memo1.Lines.Add(IntToStr(6 mod 4));
//The mod gives the remainder from the division.
//parenthesis
Memo1.Lines.Add( IntToStr(1+1+(-3)) );
//String operators
Memo1.Lines.Add('Hello' + 'World');
Memo1.Lines.Add('Hello' + 'World' + 'Again !');
//The binary number can be specified by preceding it with a percent sign (%).
abyte:= %11101000; // % : binary input 11101000 = 232 decimal
Memo1.Lines.Add('Print abyte like binary: ' + binStr(abyte,8));
Memo1.Lines.Add('Print abyte like decimal: ' + IntToStr(abyte));
//Bitwise and
bbyte:= 0;
bbyte:=abyte and %00110000; {mask tecnique select onlt 3rd and 4rh element
bbyte will become 00100000}
Memo1.Lines.Add('Print bbyte AND like binary: ' + binStr(bbyte,8));
Memo1.Lines.Add('Print bbyte AND like decimal: ' + IntToStr(bbyte));
//Bitwise or
bbyte:= 0;
bbyte:=abyte or %00110000; {set 3rd and 4rh element high(1) and leave
unaltered the rest.
bbyte will become 11111000}
Memo1.Lines.Add('Print bbyte OR like binary: ' + binStr(bbyte,8));
Memo1.Lines.Add('Print bbyte OR like decimal: ' + IntToStr(bbyte));
//Bitwise not
bbyte:= 0;
bbyte:=not abyte; {0->1 and 1->0}
Memo1.Lines.Add('Print bbyte NOT like binary: ' + binStr(bbyte,8));
Memo1.Lines.Add('Print bbyte NOT like decimal: ' + IntToStr(bbyte));
//Bitwise shl //abyte:= %11101000;
bbyte:= 0;
bbyte:= abyte shl 3; {shift left three positions}
Memo1.Lines.Add('Print bbyte SHL like binary: ' + binStr(bbyte,8));
Memo1.Lines.Add('Print bbyte SHL like decimal: ' + IntToStr(bbyte));
//x shl y = x * 2^y is valid : no overflow
abyte:= 10;
bbyte:=0;
bbyte:= abyte shl 3;
Memo1.Lines.Add('Print bbyte SHL like binary: ' + binStr(abyte,8));
Memo1.Lines.Add('Print bbyte SHL like decimal: ' + IntToStr(bbyte));
//x shl y = x * 2^y is not valid : overflow
abyte:= 100;
bbyte:=0;
bbyte:= abyte shl 3;
Memo1.Lines.Add('Print bbyte SHL overflow like binary: ' + binStr(abyte,8));
Memo1.Lines.Add('Print bbyte SHR overfflow like decimal: ' + IntToStr(bbyte));
//x shl y = x * 2^y is valid : no overflow
ainteger:= 100;
binteger:=0;
binteger:= ainteger shl 3;
Memo1.Lines.Add('Print binteger SHL like binary: ' + binStr(ainteger,16));
Memo1.Lines.Add('Print binteger SHL like decimal: ' + IntToStr(binteger));
//Bitwise shr ;
abyte:= %11101000;
bbyte:= 0;
bbyte:= abyte shr 3; {shift right three positions}
Memo1.Lines.Add('Print bbyte SHR like binary: ' + binStr(bbyte,8));
Memo1.Lines.Add('Print bbyte SHR like decimal: ' + IntToStr(bbyte));
end;
The operators example can be downloaded from here.
In the next article we will talk about decisions and loops.
Part 1: Introduction - Part 2: Hello World - Part 3: Data types, constants and variables - [[ Part 4: Operators ]] - Part 5: Decisions and Loops - Part 6: Procedures and Functions - Part 7: Custom datatypes - Part 8: Enumerations, subranges and sets - Part 9: Records