previous contents index next
(..Operators..) Contents Index (..Operators..)

2.7.3 Binary Operators (ShiftOp, BitOp)

Furthermore there also exist binary operators in SCARLET, which process integer expressions thus elements of ZZ. Therefore they strictly make use of the value's internal binary representation as a 32-bit number. (For further explanation it will be useful to think of these binary numbers as written down in a scheme with 32 fields, in which the one at the left end is used for the sign (+ corresponds to 0, - to 1) and the remaining 31 bits to show the number itself.) We distinguish between shift and logical operators

ShiftOp:
<< Shift to the left
>> Shift to right

BitOp:
& bitwise AND
| bitwise inclusive OR

The shift operators in SCARLET work like the corresponding operators in C for signed variables.

IntExpression1 << IntExpression2

means that the bit pattern of IntExpression1 is IntExpression2 positions shifted to the left. In fact all bits "dropping out" of the left side of the scheme are lost, while the scheme is filled up with zeros behind the shifted number. IntExpression2 must always be positive and less than the width of IntExpression1 regarded as a binary number.
For the shift to the right the situation is very similar:

IntExpression1 >> IntExpression2

IntExpression1 is shifted IntExpression2 positions to the right, but here the sign of IntExpression1 decides which bit is used for the fill-up: 0 if the sign is positive and 1 if it is negative.


Example 10:

11 << 4             leadsto  176
(00 ... 0000001011) leadsto  (00 ... 0010110000)

-14 >> 1            leadsto  -1.073.741.830
(1000 ... 001101)   leadsto  (1100 ... 000110)

Looking at the binary operators & and |, the binary representation is interpreted as a sequence of logical values; every 1 is taken as TRUE, every 0 as FALSE, so both expressions are bitwise combined with the logical AND or OR respectively, and the results of all single comparisons are returned as 0 or 1 again.


Example 11:

13 & -7 leadsto 5

                        00 ... 01101
                      & 10 ... 00111
                     ---------------
                        00 ... 00101

The results of all binary operations are again integer expressions.


previous contents index next
(..Operators..) Contents Index (..Operators..)