pTAL Guidelines for TAL Programmers
Coding Guidelines
pTAL Guidelines for TAL Programmers—527256-002
2-56
Signed (Arithmetic) Left Shifts
Signed (Arithmetic) Left Shifts
Guideline: The sign bit is not preserved by signed (arithmetic) left shifts.
In TAL, the signed left-shift operator (<<) performs an arithmetic left shift, shifting left all
of the bits in a 16-bit word except the left-most bit (the sign bit). The left-most bit retains
the value it had before the shift. The unsigned left-shift operator ('<<') shifts all bits
including the left-most bit of the 16-bit word.
pTAL does not support an arithmetic left shift because native architecture does not
provide efficient support for arithmetic left shifts. In pTAL (and in accelerated TAL), a
signed left-shift operator is implemented as an unsigned left-shift operator (logical left
shift).
To ensure that your program gets the same results (on overflow), whether you
accelerate it or not and whether you run it as a TNS process or as a native process,
replace each signed left-shift operator (<<) with an unsigned left-shift operator ('<<').
The results of a signed and unsigned left shift are the same, unless the signed shift
overflows (the most significant bit is shifted off the end of the word). Example 2-68 on
page 2-56 and Example 2-69 on page 2-56 show the differences.
If your code requires an arithmetic left shift, you can use the procedures in
Example 2-69 on page 2-56 in both TAL and in pTAL.
Example 2-68. Arithmetic Left-Shift Operator (TAL Only)
%000003 << 13 = %060000 = @000003 '<<' 13
%000003 << 14 = %040000 @000003 '<<' 14 = %140000
%177774 << 13 = %100000 = @177774 '<<' 13 = %100000
%177774 << 14 = %100000 @177774 '<<' 14 = 0
Example 2-69. Signed Left-Shift Procedures (page 1 of 2)
INT PROC ashift16(a, cnt); ! 16-bit signed left shift
INT a, cnt;
BEGIN
STRUCT s = a;
BEGIN
UNSIGNED(1) sign_bit;
UNSIGNED(15) rest;
END;
s.rest := s.rest '<<' cnt;
RETURN a;
END;