Operator
|
Meaning
|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
All operators return results of the same width as their operands. Thus, all overflow bits are lost. To obtain carry out information extend the input values. For example:
unsigned 4 x = 4;
unsigned 4 y = 8;
unsigned 5 z;
unsigned 4 sum;
unsigned 1 carry;
z = (0 @ x)+(0 @ y); //Extend the width to get
sum = z <- 4; //carry out information
carry = z[4];Precedence of operators is as expected from conventional C.
See also: Precedence summary
Operator
|
Meaning
|
<< |
Shift left |
>> |
Shift right |
<- |
Take LSBs |
\\ |
Drop LSBs |
@ |
Concatenate bits |
[ ] |
Bit selection |
width( expression) |
Width of expression |
unsigned int 8 x = 0b11110000;
int 4 z;
x = x >>1; // x = 0b01111000
z = x <-4; // z = 0b0000
z = x \\4; // z = 0b1111
y = x[7:4] @ x[3:0]; // y = 0b00001111
z = width(x); // z = 8Arguments for
>>, <<, <-, \\, [ ]
must be compile time constants.See also: Precedence summary
Handel-C provides the conditional expression construct familiar from conventional C. Its format is:
Expression ? Expression : Expression
The first expression is evaluated and if true the whole expression evaluates to the second expression. If the first expression is false, the whole expression evaluates to the third expression. For example:
x = (y > z) ? y : z;
See also:
if...else
, switch, select
, Precedence summary
In addition to all the operators listed in the previous sections, Handel-C provides two extra operators for expressions consisting only of compile time constants. These are:
Operator
|
Meaning
|
/ |
Division |
% |
Modulo arithmetic |
See also: Precedence summary
Operator
|
Meaning
|
&& |
Logical AND |
|| |
Logical OR |
! |
Logical NOT |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
See also: Precedence summary
The following table lists all operators in the Handel-C Language. Entries at the top have the highest precedence and entries at the bottom have the lowest precedence. Entries within the same group have the same precedence.
Operator
|
Meaning
|
array[constant] expression[constant] expression[constant:constant] RAM[expression] |
Array subscripting Bit Selection Bit range extraction RAM/ROM subscript |
! expression ~expression |
Logical NOT Bitwise NOT |
-expression | Unary Minus |
(type) expression | Type casting |
expression <- constant expression \\ constant |
Take LSBs DROP LSBs |
expression * expression constant / constant constant % constant |
Mutiplication Division Molulo arithmetic |
expression + expression expression - expression |
|
expression << constant expression >> constant |
Shift left Shift right |
expression @ expression | Concatenation |
expression < expression |
Less than Greater than Less than or equal Greater then or equal |
expression == expression expression != expression |
Equal Not Equal |
expression & expression | Bitwise AND |
expression ^ expression | Bitwise XOR |
expression | expression | Bitwise OR |
expression && expression | Logical AND |
expression || expression | Logical OR |
expression ? expression : expression | Conditional Selection |
width(expression) | Width of expression |
select(constant, expression, expression) | Compile-time selection |
Operator
|
Meaning
|
== |
Equal |
!= |
Not Equal |
< |
Less Than |
> |
Greater Than |
<= |
Less Than or Equal |
>= |
Greater Than or Equal |
See also: Precedence summary
Type casting in Handel-C is done in the same way as in conventional C code. Before variables of differing types are assigned, they have to casted to compatible types.
However, casting cannot be used to change the width of values. Inorder to change the width of the variable, the compiler must be told how to pad the variable. It can then be casted to a different type.
int 4 x;
unsigned int 4 y;
int 8 z;
x = y; // Illegal
x = (int 4) y;
z = (int 8) x; // Illegal
z = (int 8) (1@x); // x is extended to 8 bits, with 1 in the 4 MSB
z = (int 8) (x@0); // x is extended to 8 bits, with 0 in the 4 LSB